Python string features

Source: Internet
Author: User

In addition to numeric values, python can operate strings in the following aspects. Enclosed in single or double quotation marks:

>>> 'Spam eggs'
'Spam eggs'
>>> 'Doesn \' T'
"Doesn' t"
>>> "Doesn' t"
"Doesn' t"
>>> '"Yes," he said .'
'"Yes," he said .'
>>> " \" Yes, \" He said ."
'"Yes," he said .'
>>> '"Isn \' T, "she said .'
'"Isn \' T, "she said .'

Strings can be written into multiple rows. It can be represented by \ n. The next line is a logical continuation line, and the last character is indicated by a backslash:

Hello = "This is a rather long string containing \ N \
Several lines of text just as you wocould do in C. \ N \
Note that whitespace at the beginning of the line is \
Significant ."

PrintHello

Strings can be enclosed in a pair of triple quotes:

Print """
Usage: thingy [Options]
-H display this usage message
-H hostname to connect
"""

Strings can be connected together, and the "+" operator is used to repeat *:

>>> Word = 'Help' + 'A'
>>> Word
'Helpa'
>>> '<' + Word * 5 + '>'
'<Helpahelpahelpahelpahelpa>'

Two adjacent strings are automatically connected:

>>> 'Str' 'Ing' # <-This is OK
'String'
>>> 'Str' . Strip () + 'Ing' # <-This is OK
'String'
>>> 'Str' . Strip () 'Ing' # <-This is invalid
File "<Stdin>" , Line 1 , In ?
'Str' . Strip () 'Ing'
^
Syntaxerror: Invalid Syntax

Note: The content of the word string is:"Helpa"It can be the same as subscript (INDEX) and C. the first character of the string is subscript (INDEX) 0. You can specify the substring slice flag to separate the two indexes by colons.

>>> Word [ 4 ]
'A'
>>> Word [ 0 : 2 ]
'He'
>>> Word [ 2 : 4 ]
'Lp'

The default value can be used for slice indexes. The default value is zero for the first index and the default size for the second index.

>>> Word [: 2 ] # The first two characters
'He'
>>> Word [ 2 :] # Everything character t the first two characters
'Lpa'

Different from the C string, the python string cannot be changed. An error occurs when you want to modify the string at the specified index location:

>>> Word [ 0 ] = 'X'
Traceback (most recent call last ):
File "<Stdin>" , Line 1 , In ?
Typeerror: object doesn 'T support item assignment
>>> Word [: 1 ] = 'Splat'
Traceback (most recent call last ):
File "<Stdin>" , Line 1 , In ?
Typeerror: object doesn 'T support slice assignment

However, creating a new string is simple and effective:

>>> 'X' + Word [ 1 :]
'Xelpa'
>>> 'Splat' + Word [ 4 ]
'Splata'

Here is a useful slicing operation: [:] + [:] equals.

>>> Word [: 2 ] + Word [ 2 :]
'Helpa'
>>> Word [: 3 ] + Word [ 3 :]
'Helpa'

The index can be a negative number and starts counting from the right side. For example:

>>> Word [- 1 ] # The last character
'A'
>>> Word [- 2 ] # The last-but-one character
'P'
>>> Word [- 2 :] # The last two characters
'Pa'
>>> Word [:- 2 ] # Everything character t the last two characters
'Hel'

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.