Python Common String Functions

Source: Internet
Author: User
character-case transformations in strings

1. Str.lower ()//lowercase

>>> ' skate '. Lower ()
' Skate '

2. Str.upper ()//uppercase

>>> ' skate '. Upper ()
' Skate '

3. Str.swapcase ()//Case Swap

>>> ' skate '. Swapcase ()
' Skate '

4. Str.title ()//capital letter, remaining lowercase

>>> ' skate '. Title ()
' Skate '

The alignment of the string at output

1. Str.ljust (Width,[fillchar])//output width characters, str left-aligned, the insufficient portion is filled with Fillchar, the default is a space

>>> ' skate '. Ljust (10)
' Skate '
>>> ' skate '. Ljust (10, ' 0 ')
' skate00000 '

2. Str.rjust (Width,[fillchar])//output width characters, str right-aligned, the insufficient portion is filled with Fillchar, the default is a space

>>> ' skate '. Rjust (10, ' 0 ')
' 00000skate '
>>> ' skate '. Rjust (10)
' Skate '

3. Str.center (Width,[fillchar])//center alignment

>>> ' skate '. Center (10)
' Skate '
>>> ' Skate ' center (10, ' 0 ')
' 00skate000 '

4. Str.zfill (width)//The str into a width length, and right alignment, the less part with 0 complement

>>> ' skate '. Zfill (10)
' 00000skate '

String Search Related

Str.find (' t ')//search for specified string, no return-1
Str.find (' t ', start)//Specify start position search
Str.find (' t ', start,end)//Specify start and end position search:
Str.rfind (' t ')//search from the right
Str.count (' t ')//How many of the specified strings have been searched:

eg

>>> ' skate '. Find (' t ')
3
>>> ' skate '. Find (' t ', 2)
3
>>> ' skate '. Find (' t ', 2,4)
3
>>> ' skate '. RFind (' t ')
3
>>> ' skate '. Count (' t ')
1

String substitution

Str.replace (' old ', ' new ')//replace old as new
Str.replace (' old ', ' new ', maxreplacetimes)//Replace the specified number of old with new

eg

>>> ' skateskate '. Replace (' s ', ' s ')
' Skateskate '
>>> ' skateskate '. Replace (' s ', ' s ', 1)
' Skateskate '
>>>

String go to space and go to specify character

Str.strip ([chars])//Go to both sides of the chars, the default is a space
Str.lstrip ([chars])//go to the left chars, the default is a space
Str.rstrip ([chars])//Go to the right chars, the default is a space

Segmentation of strings

Str.split ([Sep, [maxsplit]])//with Sep as a delimiter, divide str into a list. Maxsplit represents the number of splits. The default delimiter is a blank character
Str.rsplit ([Sep, [Maxsplit]])
Str.splitlines ([keepends])//dividing str into a list,keepends is a bool value, and the row separator is preserved after each row is true.

eg

>>> ' Skateskate '. Split ()
[' Skateskate ']
>>> ' Skateskate '. Split (' E ')
[' Skat ', ' skat ', ']

>>> ' Skate skate '. Rsplit (")
[' Skate ', ' skate ']

>>> ' skate\n skate1 '. Splitlines ()
[' Skate ', ' skate1 ']
>>> ' skate\n skate1 '. Splitlines (1)
[' skate\n ', ' skate1 ']
>>>

String connection

Str.join (SEQ)//The sequence represented by SEQ (string sequence), connected with Str

eg

>>> ' skate '. Join (' 111 ')
' 1skate1skate1

String judgments

Str.startwith (Prefix[,start[,end])//whether to start with prefix
Str.endwith (Suffix[,start[,end])//whether end with suffix
Str.isalnum ()//is full of letters and numbers, and has at least one character
Str.isalpha ()//is full of letters and has at least one character
Str.isdigit ()//Is full number, and has at least one character
Str.isspace ()//is full of whitespace characters and has at least one character
Str.islower () whether the letters in//STR are all lowercase
Str.isupper () whether the letters in//STR are uppercase
Str.istitle ()//str If the letter is first capitalized

eg

>>> ' skate '. StartsWith (' s ')
True
>>> ' skate ' startswith (' s ')
False
>>> ' skate ' endswith (' s ')
False
>>> ' skate '. EndsWith (' e ', up)
False
>>> ' skate '. EndsWith (' e ', 1)
True
>>>

True
>>> ' skate '. Isalnum ()
True
>>> ' 222 '. Isalnum ()
True
>>> ' skate222 '. Isalnum ()
True
>>> ' Skate 222 '. Isalnum ()
False
>>>

Above this python commonly used string function in detail is the small part to share the whole content of everyone, hope to give you a reference, but also hope that we support topic.alibabacloud.com.

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.