String input
Python uses the input generally in two ways, input()
and raw_input()
, the difference is that the former can only enter a number, the latter input is a string, 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) <ipython-input-228-25ede6ea20bf> in <module> ()----> 1 input () < String> in <module> () Nameerror:name ' d ' are not Definedin [229]: Input () 23out[229]: 23In []: Input ("Input a Nu M ") input a num444out[230]: 444In [231]: n = input () 23In [232]: nout[232]: 23In [233]: s = raw_input (" Input sth.: ") input S Th.: 123In [234]: sout[234]: ' 123 ' in [235]: s = raw_input ("Input sth.:") input sth.: Sssin [236]: sout[236]: ' SSS '
String output
The output can print
be used, the following variables can be added, or directly with "," and "to include the string, using the example is as follows:
can be used under normal circumstances, you can use one containing a string, the string can contain another (but can not contain the same, otherwise need to escape)
In [241]: print "I ' m Tom" I ' m tomin [242]: print ' abc ' abcin [243]: print "abc" abcin [244]: print "abc" abcin [245]: Prin T ' "HHH" ' "HHH" in [246]: print "' Hello World '" ' Hello World ' in [247]: print ' i ' am bt ' File ' <ipython-input-247-efa01 090d6c6> ", line 1 print ' i ' am bt ' ^syntaxerror:invalid syntax# string Escape in [248]: print ' i\ ' m bt ' i ' M btin [249]: P Rint ' i ' Tom, "Hhhe", "I m Tom," Hhhe "
in [+]: 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# there is a blank line # output non-escaped string in [[]: Print R "I\ ' m Tom "I\ ' m Tom
Numeric String conversions
Direct use str()
or int()
can, nothing to say, as follows:
in [n]: Print R "I\ ' m Tom" I\ ' m tomin [257]: n = raw_input () 123In [258]: nout[258]: ' 123 ' in [259]: n = Int (n) in [260]: NOu T[260]: 123In [261]: STR (n) out[261]: ' 123 '