How Python formats strings
This article mainly introduces Python how to format strings, the article is very detailed, has a certain reference value, interested friends must read it!
Code implementation
# formatting string
A = 'hello'
Print (a)
A = 'abc' +' ' + ''
Print (a)
A = 123
Print (a)
B = 'Hello% slots%' Sun WuKong'
Print (b)
B = 'hello% s Hello% sworn% (' tom',' Sun WuKong')
Print (b)
B = 'hello% 3.5s% duration abcdefg' #% 3.5s string length is limited to 3-5
Print (b)
B = 'hello% s = 3.456
Print (b)
B = 'hello% .2f' 3.456
Print (b)
B = 'hello% dice 3.95
Print (b)
B = 'hehe'
Print (b)
C = f'hello {a} {b}'
Print (frankc = {c}')
Code run output:
Hello
Abc. Haha.
one hundred and twenty three
Hello Sun WuKong
Hello, hello tom, Sun WuKong.
Hello abcde
Hello 123.456
Hello 123.46
Hello 123
He he
C = hello 123.How!
Program analysis
# addition can also be performed between strings
# if two strings are added, the two strings are automatically concatenated into one
# string cannot be added with other types. If you do, an exception will occur: TypeError: must be str, not int
# print ("a =" + a) is not common in Python
# when creating a string, you can specify a placeholder in the string
#% s represents any character in a string
#% f floating point placeholder
#% d integer placeholder
# format string, you can create a format string by adding an f before the string
# variables can be embedded directly in a formatted string
The above is all the content of the article "how to format strings in Python". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!