Python basics 2-string formatting

Source: Internet
Author: User
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 )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.