Python 0 basics 2 --- string formatting, while learning about string formatting and output in the last night, it solves the problem that variables and strings cannot be output together in yesterday's guess number game. In fact, yesterday I forcibly converted the int type to the string type in order to output the remaining opportunity of the guess number, then we can splice the output. this is also a solution, but it is quite troublesome. today we will briefly talk about string formatting, and we will learn more about this in the future.
String output
String output is the simplest. you can use double quotation marks or single quotation marks to pack the output strings and print them directly.
Finally, the source code of the contact is attached.
# Strings and strings can be directly connected using a + connector a = "520" B = "1314" print (a + B ); # The variable can directly output number = 12 print ("a total of % d chicken" % number) # here the content of % d will be replaced by the data after %, and % d can only replace the integer numbertostr = str (number) # The forced type conversion is used here to convert the integer to the string print ("a total of" + numbertostr + "chicken ") # output a = 1.0 print using string concatenation ("The size of floating point number a is % f" % a) # Replace a floating point number with % f, print with 6 decimal places is reserved by default ("The floating point number with two decimal places reserved is %. 2f "% a) str1 =" I'm a handsome guy "print (" he said: % s "% str1) # % s can be used to replace a string # when multiple parameters are entered at the same time, in real time, score = 85.7age = 12 name = "Guo" print ("My name is % s, and my age is % d, my score is: %. 1f "% (name, age, score) # about line breaks str2 =" I love you "for I in str2: print (I) # wrap output for m in str2: print (m, end = "") # Add end = "" to avoid auto wrap output
The above is the string formatting content of Python basic 2-. For more information, see PHP Chinese network (www.php1.cn )!