Use print to output
- String
- Integer
- Floating Point Number
- Output and precision control
Strhello ='HelloPython' PrintStrhello# Output result: HelloPython# Directly output a string
1. format the output integer
PythonPrint also supports parameter formatting, which is similar to C-language printf,
Strhello ="The length of (% s) is % d" %('Hello world',Len('Hello world'))PrintStrhello# Output result: the length of (Hello World) is 11
2. Format and output a 16-digit integer
Nhex = 0x20# % X --- hex hexadecimal# % D --- decimal# % D --- Oct octal Print "Nhex = % x, ndec = % d, noct = % O" %(Nhex, nhex, nhex) # Output result: nhex = 20, ndec = 32, noct = 40# Print the same number in each integer format
3. format the output floating point number (float)
Import Math # Default Print "Pi = % F" % Math . Pi # Width = 10, precise = 3, align = left Print "Pi = % 10.3f" % Math . Pi # Width = 10, precise = 3, align = rigoal Print "Pi = %-10.3f" % Math . Pi # Prefill characters Print "Pi = % 06d" % Int ( Math . Pi ) # Output result # Pig = 3.141593 # Pig = 3.142 # Pig = 3.142 # Pig = 000003 # Formatting, precision, degree, and
4. format the output string (string)
# Precise = 3Print "%. 3 S" % ("Jcodeer")# Precise = 4Print "%. * S" % (4,"Jcodeer")# Width = 10, precise = 3Print "% 10.3 S" % ("Jcodeer")# Output result:# Jco# Jcod# Jco# Similar to strings, there are also precision, degree, and.
5. List)
L =[1,2,3,4,'Jcodeer']PrintL# Output result: [1, 2, 3, 4, 'jcodeer']# Print the list directly.'''6. Dictionary (dictionary )'''D ={1:'A',2:'B',3:'C',4:'D'}PrintD# Output result: {1: 'A', 2: 'B', 3: 'C', 4: 'D '}# SamePythonIt also supports dictionary output.
6. Print automatic line feed
Print automatically adds a carriage return at the end of the row. If you do not need to press enter, you only need to add a comma (,) at the end of the print statement to change its behavior.
ForIIn Range(0,5):PrintI,
Or directly use the following function for output:
Sys.Stdout.Write("Output string")
Reprinted statement:This article is transferred fromHttp://www.pythonclub.org/python-basic/print