Formatted string hell - Python

117 views Asked by At
x = f'{1}{2}{3}'

a = "one"
b = "two"
c = "three"

#I want to print "onetwothree"

I don't even know how to explain this problem, I froze up every time I started typing anything into google. Haven't tried anything yet, since I don't even know where to start. Help.

3

There are 3 answers

2
Barmar On

Put the variable names into the f-string.

print(f"{a}{b}{c}")
2
yoyoog On

You can insert the variables' name inside the brackets of the f-string.

a = "one"
b = "two"
c = "three"

print(f'{a}{b}{c}')
>>> onetwothree
0
Ka Wa Yip On
x = f'{1}{2}{3}'

a = "one"
b = "two"
c = "three"

#I want to print "onetwothree"
print(f"{a}{b}{c}")

enter image description here

Explanation: for every variable x, you fill in with {x}. For python 3, you use the print(item) function if you want to print out an item.

The print() function prints the specified message to the screen, or other standard output device. The message can be a string, or any other object, the object will be converted into a string before written to the screen.