Summary of common functions for strings in Python3

Source: Internet
Author: User

1. Join () connection string array. Generates a new string from a string, tuple, or element in the list, with the specified character (delimiter) connection.

Syntax: ' Sep '. Join (SEQ)

Parameter description:
Sep: Delimiter, can be empty.
SEQ: The sequence of elements to concatenate, a string, a tuple, a dictionary.
The syntax above is to combine all the SEQ elements into a new string, using Sep as a delimiter.

Return value: Returns a string that is generated after each element is concatenated with the delimiter Sep.

Cases:

content=[' i ', ' love ', ' You ']

Print ("". Join (content))

The result is: I love you

2. Find () is used for string lookups, finds the desired substring in a given string, returns the leftmost index where the substring is located, and no return-1 is found.

Syntax: Str.find ("str1", Startnum,endnum)

Parameter description:

STR: the given string.

STR1: the substring to find.

Startnum: The index value at which to start the lookup.

Endnum: The index value of the end lookup.

Cases:

Str= "I Love You"

S=str.find ("V", 2,8)

Print (s)

Showing the result: 4

3. The Strip () method is used to remove the character specified by the tail of the string (the default is a space) or a sequence of characters.

Note: This method can only delete characters at the beginning or end, and cannot delete the middle part.

Syntax: Str.strip (' char ') where char is the specified character, the default is a space.

Cases:

Str= "****i Love you***"

Print (Str.strip (' * '))

The result is: I love you

Extension: the Lstrip () function removes the specified character from the left, and Rstrip () removes the character specified on the right.

4, Split () slices the string by specifying a delimiter, and separates only the NUM substring if the parameter num has a specified value.

Syntax:str. Split(str1, num)

Parameters:

The str1 delimiter, which defaults to all empty characters, including spaces, line breaks (\ n), tabs (\ t), and so on.

Num Split count.

Cases:

str = "This is string example....wow!!!"

Print (str. Split( ))

Print (Str.split (' I ', 1))

Print (Str.split (' W '))

The results shown are:

[' This ', ' is ', ' string ', ' example....wow!!! ' ]

[' th ', ' s is string example....wow!!! ' ]

Span class= "pun" >< Span class= "PLN" >< Span class= "PLN" >< Span class= "pun" >[ ' This is string example .... ' , ' o ' , ‘!!!‘ /span>

5. Upper () converts all given strings to uppercase

6. Lower () converts all given strings to lowercase

7, replace () string substitution function, replace () method replaces the old string in the string with new (the string), if the third parameter max is specified, the substitution does not exceed Max times.

Syntax: Str.replace (old, new[, Max])

Parameters:
Old--the substring to be replaced.
New-A string that replaces the old substring.
Max--Optional string, replace no more than Max Times
return value
Returns a new string that is generated after the old (older string) in the string is replaced with new (the newly created string), and if the third parameter max is specified, the substitution does not exceed Max times.

Cases:

Str= "He Love You"

Print (Str.replace (' He ', ' I '))

The result is: I love you

Summary of common functions for strings in Python3

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.