Python "Two" python string manipulation

Source: Internet
Author: User

Python's string manipulation is flexible; Let's look at an example:

Str= ' HelloWorld '

First we output this string:

Print STR This is a representation method, and there are many ways to express it;

Like what:

Print Str[0:10]

We can get HelloWorld, too.

Print Str[1:3]

We get: El, intercept the string of El two characters;

Pre-requisites for intercepting strings:

That's it. If we want to intercept a certain character, we should first find out the starting position of the character, and then we can intercept it correctly, and provide the method of string lookup in Python, which is similar to the character lookup in C language; the find () method;

For example, we look for wo this string;

PoN =str.find (' wo ')

We will get 5, that is, the first character of Wo appears in position 5th of the string. So we can intercept 2-length characters from position 5th, and we'll get to the WO string we want.

The

5:7 indicates the length of the truncated string; 2

Print Str[5:7] The result is wo

Python's alternative operations on strings:

For example, if we want to output the last character of the above string, we just need to do the following:
Print Str[-1] or print str[10] This needs to know the length of the string to be able;

We will get d this character;

We can also get all the characters except the last character:
Print Str[:-1] we get helloworl;

Concatenation of strings in Python:

Of course, the string concatenation and substitution of the premise is to find the string is to use the Str.find () function;

How to splice it? You can use the + sign operator in Python to stitch between strings

Like what:

Str1= ' We are '

Str2= ' Good friends '

str3=str1+ ' +STR2

Print STR3

We get we are good friends

The non-variability of the Python string:

The string is immutable in Python--after the string is created, its internal values cannot be changed, and this and C + + has a much larger change in the operation of the string;

So how do we change the value of the interior?
Or the HelloWorld above, I want him to become Hellozorld string, how does this happen?

We can do this:

Str= ' HelloWorld '

Str1= ' Z '

Str2=str[0:5]+str1

Str3=str[6:]

Str4=str2+str3

Print STR4

We got Hellozorld.

character substitution in Python :

As we did above, I did not use the substitution function provided by Python, in fact Python has a replacement function, that is, the replace () method

Like what:

Str= ' HelloWorld '

Str2=str[1:5]

str3= ' Call '

Str4=str.replace (STR2,STR3)

Print STR4

We get hcallworld, this substitution method has a return value, because the string inside cannot be changed.

Gets the length of the string in Python:

Print Len (str)

Uppercase and lowercase translation of strings in Python;

Str= ' HelloWorld '

Print Str.upper ()

We're going to get HelloWorld.

Python "Two" python string manipulation

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.