Python Learning Note Five: string methods

Source: Internet
Author: User

Common string constants:

string . Digits : Contains numbers 0~9 the string

string . Letters : Contains all letters (uppercase or lowercase strings, python3.0 , use string.ascii-letters instead)

string . Lowercase : A string containing all lowercase letters

string . Printable : A string containing all printable characters

string . Punctuation : A string that contains all the punctuation

string . Uppercase : A string containing all uppercase letters

1 ) Find : Finds a substring in a longer string, returns the leftmost index where the substring is located, and returns if not found - 1

>>>title = "Monty Python ' s Flying Circus"

>>>title.find (' Monty ')

0

>>>title.find (' Python ')

6

Accept the optional start and end points parameters:

>>>subject = ' $$$ Get rich now!!! $$$ '

>>>subject.find (' $$$ ', 1)

21st

>>>subject.find ('!!! ', 1,16)

-1

2 ) Join : Used to concatenate elements in a sequence, and must be a string, syntax format: Connector . Join ( string )

>>>seq = [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']

>>>sep = ' + '

>>>sep.join (seq)

' 1+2+3+4+5 '

>>>dir = ', ' usr ', ' bin ', ' env '

>>> '/'. Join (dir)

'/usr/bin/env '

3 ) Lower : Returns the lowercase letter of a string

>>> ' Trondheim Hammer Dance '. Lower ()

' Trondheim Hammer Dance '

>>>

>>>

>>> name = ' Gumby '

>>> names = [' Gumby ', ' Smith ', ' Jones ']

>>> if Name.lower () in Names:print ' Found it '

...

Found it

Title Conversion:

title method: Capitalize all the words in the first letter, while the other letters are lowercase

>>> "That's All Folks". Title ()

"That ' SAll folks"

>>>

Capword function:

>>>import string

>>>string.capwords ("That's All Folks")

"That ' sAll folks"

4 ) Replace : Returns the string after all occurrences of a string have been replaced, that is, the lookup substitution

>>> ' This was a test '. Replace (' is ', ' EEZ ')

' Theezeez a test '

5 ) Split : Join inverse method used to split a string into a sequence

>>> ' 1+2+3+4+5 '. Split (' + ')

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

6 ) Strip : Returns a string that strips both sides (with no interior) space (by default)

>>> ' internal whitespace krpt '. Strip ()

' Internalwhitespace krpt '

Specify delimiter:

>>> ' * * * SPAM * for * everyone!!! '. Strip (' *! ')

' spam* for * everyone '

7 ) Translate : Replacing portions of a string with only a single character, with the advantage that you can make multiple substitutions at the same time, before replacing it, you need to complete a conversion table and use string module's Maketrans function

>>>from String Import Maketrans

>>>table = Maketrans (' cs ', ' KZ ')

>>>len (table)

256

>>> ' This was an incredibletest '. Translate (table, ') # The second parameter is optional, specifying the character to be deleted

' Thizizaninkredibletezt '


Python Learning Note Five: string methods

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.