String Formatting

f-strings, format(), % formatting

Interview Relevant: Modern Python practices

String Formatting

F-strings (Python 3.6+) are the preferred way.

Code Examples

Formatting strings.

python
1name = "Alice"
2age = 25
3
4# f-strings (Best)
5print(f"{name} is {age} years old.")
6
7# format() method
8print("{} is {} years old.".format(name, age))
9
10# % operator (Old)
11print("%s is %d years old." % (name, age))

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In