Python String Usage Learning notes

Source: Internet
Author: User
Tags explode php explode print format in python

String formatting:

The code is as follows Copy Code

format = "Hello%s,%s enough for ya?"
Values = (' World ', ' hot ')
Print format% values

Result: Hello world, hot enough for ya?
Note: If not executed at the command line, enclose the print behind parentheses

Similar to PHP but not the same function or method name:

explode/"target=" _blank ">php explode=> python split
PHP trim => python Strip
PHP implode => python join

A slice of a string is a substring of an intercept string . It is quite handy to intercept strings in Python. First, assign a value to a variable:

>>> word= "Hello"

The first slice operation is a string that gets the nth character of the string:

The code is as follows Copy Code
>>> Word[1]
E

Python's "subscript" is also starting from 0. Python seems to have no character in this concept, only the concept of "a string of only one character". So, we can't change a string like C by specifying an "array" subscript, for example, the following code is wrong:

The code is as follows Copy Code
>>> word[1]= ' o '

Prompt appears:


Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ' str ' object does not support item assignment

The subscript in the Python string can also be a negative number, representing the Right-to-left order. Note, however, that the string "last" character in the natural language is the first-1 character in Python. and Python thinks-0 = 0, so there are the following results:

The code is as follows Copy Code
>>> word[-0]
H
>>> Word[-1]
' O '

Python can also cut substrings that contain more than one character, separated by a colon:

The code is as follows Copy Code


>>> Word[1:3]
' El '

In fact, we can look at the subscript of the Python string as a "space between the characters", and the following figure is a better way to show the nature of the Python subscript:

The code is as follows Copy Code


+---+---+---+---+---+
| H | e | l | l | o |
+---+---+---+---+---+
0 1 2 3 4 5
-5-4-3-2-1-0

It is not difficult for you to understand the output of the following statement:

The code is as follows Copy Code


>>> Word[3:1]
''
>>> Word[1:-1]
' Ell '
>>> Word[-1:-3]
''

That is, if the subscript for Python is marked on the left, and the value on the right is marked on the right, the content between the two voids will be the value of the expression. Instead, it will be an empty string.

If a colon is written at the bottom of the string, and the side of the colon, or even the values on either side of it, are empty, it means "the most extreme value", which is to make the substring as long as possible:

The code is as follows Copy Code

>>> word[:]
' Hello '
>>> word[1:]
' Ello '
>>> Word[:-2]
' Hel '

In Python, a string substring, represented by a subscript, is read-only. And in memory, it should be a copy

Segmentation and composition of python strings

  code is as follows copy code
>>> s
' Hello world! everyone! This is my I-string! '
>>> s.split ()
[' Hello ', ' world! ', ' everyone! ', ' This ', ' are ', ' my ', ' a ', ' string! ']
>>> s.split (', 4)
[' Hello ', ' world! ', ' everyone! ', ' This ', ' are my A-string! ']
>>> s.split (' e ')
[' H ', ' Llo world! Ev ', ' Ryon ', '! This is my string! ']
>>> s.rsplit ()
[' Hello ', ' world! ', ' everyone! ', ' This ', ' are ', ' my ', ' a ', ' string! ']
>>> s.rsplit (', 4)
[' Hello world! everyone! This ', ' was ', ' my ', ' a ', ' string! '
>>> s.rsplit (' e ')
[' H ', ' Llo world! Ev ', ' Ryon ', '! This is my string! ']

#s. Split ([Sep, [Maxsplit]]) takes Sep as a separator, dividing s into a list. Sep defaults to spaces. Maxsplit is the number of partitions, the default is to split the entire s
The difference between #s. rsplit ([Sep, [Maxsplit]]) and split () is that it is split forward from the string end of S.

The code is as follows Copy Code

>> > S=s.replace (', '/n ')
>>> s
' hello/nworld!/neveryone!/nthis/nis/nmy/nfirst/nstring! '
>>> s.splitlines ()
[' Hello ', ' world! ', ' everyone! ', ' This ', ' are ', ' my ', ' a ', ' string! ']
>>> s.splitlines (True)
[' hello/n ', ' world!/n ', ' everyone!/n ', ' this/n ', ' is/n ', ' my/n ', ' first/n ', ' String! ']
>>> s.splitlines (False)
[' Hello ', ' world! ', ' everyone! ', ' This ', ' are ', ' my ', ' a ', ' string! ']
>>> '/t '. Join (S.splitlines ())
' hello/tworld!/teveryone!/tthis/tis/tmy/tfirst/tstring! '
#s. Splitlines ([keepends]) divides s into a list by the line separator. If Keepends is true, the row delimiter is reserved for each element of the list, and if False, the separator
#s is not preserved. Join (seq) concatenates the SEQ sequences with S

Related Article

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.