Take advantage of the time last night to learn a simple string of formatted output, just solve the problem in writing yesterday's guessing number game variables and strings can not be a piece of output, in fact, yesterday in order to output guessing the number of the remaining opportunity, I cast the int type into a string type, and then through the stitching output, This is also a solution, but it is more trouble, today simple to say the format of the string, and then have this knowledge, will be supplemented in time.
The output of the string
The output of the string is the simplest, wrapped directly in double quotation marks or single quotation marks that will be output, directly to print.
Note, however, that if you want to output a string that itself contains single quotes, you enclose it in double quotation marks, and you cannot use single quotes. Vice versa.
For everyone to output a string containing single or double quotation marks, Python provides you with three quotation marks, you can enter in three quotation marks you want to output the string, you do not have to worry about the problem.
2. String + Output of variables
Here is mainly the use of the substitution, commonly used in lieu of symbols similar to the C language, the following said a few commonly used.
%d-integer%s-string%f-floating-point number
Of course, similar to the C language can be controlled by the number of bits
Integers are preceded by spaces to make up the number of digits.
3. Output a string containing multiple variables
The main use of tuples here is to assign values to the parameters, which are described later in the meta-group.
4. Line break
Python is automatically wrapped by default, in order to keep it from wrapping, add end= "" to the back.
The knowledge of these string formatting is basically sufficient in Python's daily routine, and will continue to be replenished later.
Finally the source code to contact is attached.
#字符串与字符串可以直接使用 + connector to connect a= "520" b= "1314" Print (a+b), #变量可以直接输出number =12print ("Altogether%d chickens"%number) #这里%d will be replaced by the data after%, And%d can only replace the integer numbertostr=str (number) #这里使用了强制类型转换, convert the Shaping to string print ("A total" +numbertostr+ "chicken Only") #使用字符串的拼接进行输出a =1.0print (" The size of floating-point A is%f "%a") #用%f instead of a floating-point number, which retains 6 decimal places by default print ("Float with two decimal places is%.2f"%a) str1= "I'm Handsome" print ("he said:%s"%str1) #%s can be used instead of a string # At the same time fill in the case of multiple parameters, in fact, the meta-composition of the assignment score=85.7age=12name= "Guo" Print ("My name is:%s, my age is:%d, my score is:%.1f"% (Name,age,score)) # Questions about line breaks str2= "I love You" for I in str2: print (i) #默认总时换行输出for m in str2: print (m,end= "") #加上end = "" avoids its wrap output
The above is the Python 0 basic Primer Two---the formatted content of the string, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!