The following describes the input and output of strings and strings in Python.

Source: Internet
Author: User

The following describes the input and output of strings and strings in Python.

String
The character string is enclosed by ''or" ". If the character string contains 'or", use \ to escape it.

>>> print 'I\'m ok.'I'm ok.

The Escape Character \ can be used to escape many characters. For example, \ n represents a line break, \ t represents a tab, and the character \ itself must also be escaped \. If you do not need to escape, you can use r '':

>>> print '\\\t\\'\  \>>> print r'\\\t\\'\\\t\\

If a string contains many line breaks, \ n is not easy to read in a line. To simplify it, Python allows '''... ''' Format indicates multiline content:

>>> print '''line1... line2... line3'''line1line2line3

If it is written as a program, it is:

print '''line1line2line3'''


Possible problems
Chinese Encoding Problems

# coding = utf-8

Error:

SyntaxError: Non-ASCII character ‘/xe6'

So I finally changed it

# coding=utf-8

Alas ....

Unicode encoding Problems

Python 2.7.6 (default, Mar 22 2014, 22:59:56) [GCC 4.8.2] on linux2Type "help", "copyright", "credits" or "license" for more information. >>> len ('Chinese') 6 >>> len (u'chinese') 2 >>>

Note: this problem is caused by python encoding. For details about encoding, see string and encoding. However, this encoding problem does not exist in python 3. x:

Python 3.4.0 (default, Jun 19 2015, 14:20:21) [GCC 4.8.2] on linuxType "help", "copyright", "credits" or "license" for more information. >>> len ('Chinese') 2 >>> len (u'chinese') 2 >>>
 

Output

>>> print 'hello, world'hello, world>>> print 'The quick brown fox', 'jumps over', 'the lazy dog'The quick brown fox jumps over the lazy dog>>> print '100 + 200 =', 100 + 200100 + 200 = 300

Input

>>> name = raw_input()Michael>>> name'Michael'>>> print nameMichael>>> name = raw_input('please enter your name: ')please enter your name:

Note: raw_input always returns a string. That is to say, if you enter an int type, a numeric string is returned and you need to convert it:

>>> Number = raw_input ("enter an integer:") enter an integer: 123 >>>> number '000000' >>> number = int (raw_input ("enter an integer: ") input an integer: 123 >>> number123

Articles you may be interested in:
  • Basic knowledge about string types in Python
  • Simple notes on Python string features and common string Methods
  • Implementation of Python string objects
  • Example of string operation method developed in python
  • Translate strings in the Django framework in Python
  • Introduction to using the split () method to split strings in Python
  • Introduction to the ljust () method for processing strings in Python
  • Introduction to using the islower () method to process strings in Python
  • How to Use the endswith () method to process strings in Python
  • Review string knowledge points in Python

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.