day01: How to use the print() function on the first day of Python

Table of contents

1. Basic use of print

Two, print () function prototype:

Three, print () print output text

print normally

print strings with single and double quotes (escape character \)

print windows path (r)

print string with double quotes or single quotes

print multiline string

Use of newline character \n

Use of the horizontal tab character \t

Use of separator sep

Do not wrap after printing, use the end parameter to set the end symbol you want


1. Basic use of print

The first piece of code to learn a language is hello word

Python print hello word

print("hello word")    #python代码段
hello word             #回显

Two, print () function prototype:

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

Print "objects" to the text stream specified by the "file parameter", separated by the "sep parameter" and appended with the "end parameter" at the end. "sep" , "end ", "file" and "flush" must be given as keyword arguments. The flush keyword parameter was added after version 3.3 of phthon.

parameter meaning
objects Specify the objects to be output, and use English commas (,) to separate multiple objects
sep Specifies the separator between multiple output objects, the default is a space (' ')
end Specify the final terminator, the default is a newline character ('\n')
file Specifies the file object to write to, the default is the standard output stream (sys.stdout)
flush Specifies whether to forcibly refresh the output stream, the default is not to refresh (False)

Note: If you specify  sep end file  and  flush  parameters, you need to use keyword parameters (that is, assign values ​​​​by specifying the parameter name).
 

Three, print () print output text

print normally

print("自强不息")
自强不息

print('自强不息')
自强不息

print("自强不息,厚德载物。")
自强不息,厚德载物。

Then according to the above print() function, it can be seen that there is no difference between double quotation marks and single quotation marks when outputting strings.

print strings with single and double quotes (escape character \)

When there are single quotes or double quotes in the string, direct printing may be wrong

Example: Strings with single quotes: Let's go

           Double-quoted string: I Love "You"

print('Let's go') 
     
SyntaxError: unterminated string literal (detected at line 1)

print("I Love"You"")  
    
SyntaxError: invalid syntax. Perhaps you forgot a comma?

Because Let's go also contains single quotation marks, and I Love "You" contains double quotation marks, so what should we do?

Method 1: When there are single quotation marks in the text, we use double quotation marks to wrap them, and those with double quotation marks use single quotation marks to wrap them

print("Let's go")
      
Let's go

print('I Love"You"')
      
I Love"You"

Method 2: Use the escape character \

print('Let\'s go')
      
Let's go


print("I Love\"You\"")
      
I Love"You"

print windows path (r)

Then the escape character in pyhton is \, and \ in windows is equivalent to the path

For example: C:\Users\lenovo\Desktop\Problems encountered at work are solved

we print directly

print("C:\Users\lenovo\Desktop\工作中遇到的问题解决")
      
SyntaxError: incomplete input

Why does this happen, because Python understands our path as \U, \l, \D, \work, which causes python not to understand what these mean

So what if we want to print the windows path in Python?

method one:

print("C:\\Users\\lenovo\\Desktop\\工作中遇到的问题解决")
      
C:\Users\lenovo\Desktop\工作中遇到的问题解决

Then it is very cumbersome for us to add a backslash before each backslash, so the simple method is as follows

Method 2: Add an r

print(r"C:\Users\lenovo\Desktop\工作中遇到的问题解决")
      
C:\Users\lenovo\Desktop\工作中遇到的问题解决

print string with double quotes or single quotes

Sometimes we need to print double quotes for the full text, for example: "Self-improvement" or 'Self-improvement'

You can use ('""') single quotes to wrap double quotes, ("''") double quotes to wrap single quotes

print('"自强不息"')
      
"自强不息"

print('"自强不息"')
      
"自强不息"

print multiline string

Sometimes we need to print many lines of strings

For example: Tianxingjianjunzi strives for self-improvement

           Geography Kun gentleman carries things with virtue

We can use triple quotes ("""content""")

print("""
自强不息
厚德载物
""")
      

自强不息
厚德载物

Use of newline character \n

We can also use \n to achieve newline

print("自强不息\n厚德载物")
自强不息
厚德载物

Use of the horizontal tab character \t

Horizontal tabs can regularly separate each character

print("1\t2\t3")
1	2	3	

Use of separator sep

Use the sep parameter to constrain the separator between the multiple contents in the print brackets

print("www","csdn","net")
www csdn net

print("www","csdn","net",sep=".")
www.csdn.net

Do not wrap after printing, use the end parameter to set the end symbol you want

print("自强不息","厚德载物")
自强不息 厚德载物

print("自强不息","厚德载物",end="!")
自强不息 厚德载物!

Guess you like

Origin blog.csdn.net/weixin_50877409/article/details/129422444