Python string input/output

Source: Internet
Author: User
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 usageprintYou can add variables to the backend. you can also use ", ', and ''' to include strings. The following is an example:

  • It can be used normally. one type can contain one string, and the other type can be included in the string (but the same type cannot be included; otherwise, it must be escaped)

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

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!

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.