简体   繁体   中英

Formatted string hell - Python

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.

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

Put the variable names into the f-string.

print(f"{a}{b}{c}")
x = f'{1}{2}{3}'

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

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

在此处输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM