A detailed description of the Python string input and output

Source: Internet
Author: User

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 "
    • Line break

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 '

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.