Python string method

Source: Internet
Author: User

This article is referenced from the basic Python Tutorial (second edition)

Operation Grammar Example Results

Find sub-string

 

find ("str")

1. Returns the leftmost index where the substring is located

2. No return-1

3. You can accept the start and end points parameters, the range left closed right open

title = "Monty python ' s Flying Circus"

1.title.find ("Monty")

   title.find ("Python")

2.title.find ("Zircus")

3.title.find ("Circus", 0,     #提供起始点和结束点

 

0

6

-1

-1

Adding a sequence

"Sep". Join (SEQ)

Inverse method of 1.split

1.sep = "+"

seq = ["1", "2", "3", "4", "5"]

Sep.join (seq)

2.sep = "/"

dirs = ["", "usr", "bin", "env"]

"Sep.join (dirs)

"1+2+3+4+5"

"/usr/bin/env"

Split sequence

Str.split ("Sep")

Inverse method of 1.join

1. "1+2+3+4+5". Split ("+")

2. "/usr/bin/env". Split ("/")

["1", "2", "3", "4", "5"]

["", "usr", "bin", "env"]

lowercase letters Str.lower () 1. "Trondheim Hammer Dance". Lower () "Trondheim Hammer Dance"
Capital Str.upper () 1. "Trondheim Hammer Dance". Upper () "TRONDHEIM HAMMER DANCE"
Title Conversion String.capwords ()

1.import string

String.capwords ("That's all, folks")

"That's all, folks"

Replace Str.replace (old,new) 1. "This was a test". Replace ("is", "Ezz") ' Thezz ezz a test '

Remove spaces

Str.strip ()

1. Remove both sides of the space (without internal)

2. Specify the characters that need to be removed and use them as parameters

1. "Internall whitespace is kept". Strip ()

2. "* * * * SPAM * for * everyone!!! ". Strip (" *! ")

"Internall whitespace is kept"

"SPAM * for * everyone"

Str.lstrip ()

Remove left space

1. "Internall whitespace is kept". Lstrip () "Internall whitespace is kept"

Str.rstrip ()

Remove Right space

1. "Internall whitespace is kept". Rstrip () "Internall whitespace is kept"

Python string method

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.