This article shares a detailed introduction to string input and output in Python strings.
Python generally uses two input methods,input()
Andraw_input()
The difference is that the former can only enter numbers, and the latter can input strings, using the following:
In [226]: help(input)Help on built-in function input in module __builtin__:input(...) input([prompt]) -> value Equivalent to eval(raw_input(prompt)).In [228]: input()d---------------------------------------------------------------------------NameError Traceback (most recent call last)
in
()----> 1 input()
in
()NameError: name 'd' is not definedIn [229]: input()23Out[229]: 23In [230]: input("input a num")input a num444Out[230]: 444In [231]: n = input()23In [232]: nOut[232]: 23In [233]: s = raw_input("input sth.: ")input sth.: 123In [234]: sOut[234]: '123'In [235]: s = raw_input("input sth.: ")input sth.: sssIn [236]: sOut[236]: 'sss'
String output
Output usageprint
You can add variables to the backend. you can also use ", ', and ''' to include strings. The following is an example:
In [241]: print "I'm Tom" I'm TomIn [242]: print 'ABC' abcIn [243]: print "abc" abcIn [244]: print '''abc''' abcIn [245]: print '"hhh"' "hhh" In [246]: print "'Hello World'" 'hello world' In [247]: print 'I am bt 'file"
", Line 1 print 'I am bt' ^ SyntaxError: invalid syntax # string escape In [248]: print 'I \ 'mbt' I'm btIn [249]: print ''' I'm tom, "hhhe" ''' I'm tom, "hhhe"
In [250]: print ''' I .....: am tom .....: hhha ''' iam tomhhhaIn [254]: print 'I am \.....: tom \.....: hh 'I am tom hhIn [255]: print "I \.....: am \ n \.....: tom \ n "iam tom # empty line # output non-escape string In [256]: print r" I \'m Tom "I \'m Tom
Numeric string conversion
Direct usestr()
Orint()
There is nothing to say, as shown below:
In [256]: print r"I\'m Tom"I\'m TomIn [257]: n = raw_input()123In [258]: nOut[258]: '123'In [259]: n = int(n)In [260]: nOut[260]: 123In [261]: str(n)Out[261]: '123'
The above is a detailed introduction to Python string input and output. For more information, see other related articles in the first PHP community!