Python output

51CTO 2022-05-14 15:52:00 阅读数:233

pythonoutput



Python Output

  • ​​ One 、 Preface ​​
  • ​​ Two 、 Use print() Output function ​​

One 、 Preface

Data input and output operation , Is the most basic operation of the computer . In this paper, we only study the basic input and output , Basic input refers to the operation of inputting data from the keyboard , Basic output refers to the operation of displaying the output results on the screen .

There are many common input and output devices , As shown in the figure : The camera 、 Scanner 、 Microphone 、 Keyboards, etc. are input devices , Then it is decoded by the computer and output and displayed at terminals such as display or printer . The basic input and output means that we usually input characters from the keyboard , And then on the screen .

Python Output _python


Two 、 Use print() Output function

stay Python in , Use the built-in print() Function can output the result to IDLE Or on the standard console .

print() The basic syntax format of the function is as follows :


print( Input content )



among , The output content can be numbers and strings ( The string needs to be enclosed in quotation marks ), This content will be output directly , It can also be an expression containing an operator , This kind of content will output the calculation results .

a = 100 # Variable a, The value is 100
b = 5 # Variable b, The value is 5
print( 9) # Output number 9
print( a) # Output variables a Value 100
print( a * b) # Output a*b Result 500
print( "go big or go home") # Output "go big or go home"( Or outstanding , Or out )
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

stay Python in , By default , One print() After the statement is output, it will wrap automatically , If you want to output more than one content at a time , Without changing lines , The contents to be output can be separated by commas with half width in English , The following code will output variables on one line a and b Value :

print( a, b, " Or outstanding , Or out ") # The output is :100 5 Or outstanding , Or out
  • 1.

In programming , The symbols we enter can use ASCII Enter in the form of code .ASCII Code is the American Standard Code for information exchange , At the very beginning 127 One letter is encoded into the computer . That is, English upper and lower case letters 、 Numbers and symbols , This coding table is called ASCII code . Like capital letters A The code of is 65, Lowercase letters a The code of is 97 . adopt ASCII Code display character , Need to use chr Function character conversion .

print( "a") # The output characters a
print( chr( 97)) # The output characters a

print( "A") # The output characters A
print( chr( 65)) # The output characters A

print( "B") # The output characters B
print( chr( 66)) # The output characters B

print( "+") # The output characters +
print( chr( 43)) # The output characters +

print( 8) # The output characters 8
print( chr( 56)) # The output characters 8

print( "[") # The output characters [
print( chr( 91)) # The output characters [

print( "]") # The output characters ]
print( chr( 93)) # The output characters ]
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

ASCII Code is often used in programming , Learn to master ASCII Some rules of code value . Common characters and ASCII Do you have a checklist , As shown in the figure :




ASCII value



Control characters



ASCII value



Control characters



ASCII value



Control characters



ASCII value



Control characters



0



NUL



32



(space)



64



@



96





1



SOH



33





65



A



97



a



2



STX



34





66



B



98



b



3



ETX



35



#



67



C



99



c



4



EOT



36



$



68



D



100



d



5



ENQ



37



%



69



E



101



e



6



ACK



38



&



70



F



102



f



7



BEL



39



'



71



G



103



g



8



BS



40



(



72



H



104



h



9



HT



41



)



73



I



105



i



10



LF



42



*



74



J



106



j



11



VT



43



+



75



K



107



k



12



FF



44



,



76



L



108



l



13



CR



45



-



77



M



109



m



14



SO



46



.



78



N



110



n



15



SI



47



/



79



O



111



o



16



DLE



48



0



80



P



112



p



17



DCI



49



1



81



Q



113



q



18



DC2



50



2



82



R



114



r



19



DC3



51



3



83



X



115



s



20



DC4



52



4



84



T



116



t



21



NAK



53



5



85



U



117



u



22



SYN



54



6



86



V



118



v



23



TB



55



7



87



W



119



w



24



CAN



56



8



88



X



120



x



25



EM



57



9



89



Y



121



y



26



SUB



58



:



90



Z



122



z



27



ESC



59



;



91



[



123



{undefined



28



FS



60





92



\



124



|



29



GS



61



=



93



]



125



}



30



RS



62





94



^



126



~



31



US



63



?



95





127



DEL




With the further development of computer . In the computer, not only the basic English characters need to be stored and used , You also need to store Russian 、 chinese 、 Japanese and other words or characters , Then there are many versions of information conversion coding , Such as Unicode\UTF-8 etc. .Python3.0 With Unicode Encode internal characters .Unicode Use double byte 16 Bits to encode , Programmable 65536 Characters , It basically contains all the language characters in the world , It has become a common coding method all over the world , And in hexadecimal 4 Bits represent a code , Very simple and intuitive , Accepted by most developers . Printing Chinese characters can be used directly U+ The form of coding . Such as printing Chinese characters " Biochemical crisis " and " China ", The code is as follows :

print( "\u751f\u5316\u5371\u673a") # The output characters : Biochemical crisis
print( "\u4e2d\u56fd") # The output characters : China
  • 1.
  • 2.

Use print() function , Not only can you output content to the screen , You can also output to a specified file . for example : Put a string “ Or outstanding , Or out ” Output to “D:\mr.txt” in , The code is as follows :

fp = open( r"D:\mr.txt", "a+") # Open file
print( " Or outstanding , Or out ", file = fp) # Output to a file
fp. close() # Close file
  • 1.
  • 2.
  • 3.

After executing the above code , Will be in “D:\” Generate a directory named mr.txt The file of , The contents of this document are written in “ Or outstanding , Or out ”, As shown in the figure :

Python Output _python_02


Then whether the current year can be 、 The month and date are also output ? Certainly. , But you need to call datetime modular , And the corresponding date can be output according to the specified format . If you want to output the current year and current date , The output code is as follows :

import datetime # Call the date module datetime
print( " Current year " + str( datetime. datetime. now(). year)) # Output the current year
print( " current time " + datetime. datetime. mow(). strftime( % Y - % m - % d % H: % M: % S)) # Output current time
  • 1.
  • 2.
  • 3.


版权声明:本文为[51CTO]所创,转载请带上原文链接,感谢。 https://pythonmana.com/2022/134/202205141525034180.html