1

In Python 3.5, using keyword arguments in str.format has been deprecated:

"Hi {s}".format(s="world")

From the string docs:

Deprecated since version 3.5: Passing a format string as keyword argument format_string has been deprecated.

What are the best alternatives in Python 3.5+?

1
  • Please fix the deprecation notice link Commented Jun 26, 2018 at 9:54

3 Answers 3

3

Deprecation is about string.Formatter and not about str.Formatter:

Source:

Passing a format string as keyword argument format_string to the format() method of the string.Formatter class has been deprecated.

You can use in str.format, but not in string.Formatter

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your clarification - I have misunderstood the deprecation warning. So I guess its fine to use "Hi {s}".format(s="world") going forward?
@joshlk, yes, it's fine.
1

Or use fstrings:

name = "Bob"
hello = f"Hello {name}"
print (hello)

Output:

Hello Bob

Comments

0

Try this

name = "john"
hello = "GoodMorning %s" %(name,)
print (hello)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.