Python base 5--string

Source: Internet
Author: User


1 find (), RFind (), index (), Rindex (), COUNT ()

s ="This apple is red apple"S.find ("Apple") S.find ("Apple", 9) S.find ("Apple", 1,3) S.rfind ("app")#looking forward from the end of a stringS.index ("pp") S.count ("P")

2 split (), Rsplit (), partition (), Rpartition ()

Partition divides the string into three parts, before the delimiter, the delimiter, and the ' R ' represents the forward from the tail

S.split ('# ' uses spaces to split [' This ', ' apple ', ' is ', ' red ', ' apple ']s.partition (' ')  # (' This ', ' ", ' Apple is Red apple ')

3 Join ()

Multiple string connections, adjacent strings inserted into specified characters

S1 = s.split (""-"#" This-apple-is-red-apple '

4 lower (), upper (), capitalize (), title (), Swapcase ()

Converts a string to lowercase, uppercase, first uppercase, uppercase for each word, case-by-letter

5 replace ()

S.replace ("Apple","Orange"#' This orange is red Orang '

6 Maketrans (), translate ()

Maketrans () generates a character map, translate () replaces characters by mapping table, and the second parameter is the character to be deleted

Importstringtable= String.maketrans ("ABCDEFG","1234567") s="This apple is red apple"s.translate (table)#' This 1ppl5 is r54 1ppl5 'S.translate (table,"Hijk")#remove Hijk ' ts 1ppl5 s r54 1ppl5 '

7 strip (), Rstrip (), Lstrip ()

Remove both ends, right, left whitespace characters, or specified characters

s ="ABC"S.strip ()#remove both whitespace characters ' abc '"ABDC". Strip ("a")#Delete the specified character"aabdcaaa". Rstrip ("a")#Delete the right end of the specified character ' AABDC '

8 eval ()

Attempt to convert any character to an expression for evaluation

Eval ("3+4")import  matheval ('math.sqrt (3)  ')

9 StartsWith (), EndsWith ()

Determines whether a string starts or ends with a specified string

Isalnum (), Isalpha (), IsDigit (), Isspace (), Isupper (), Islower ()

Tests whether a string is a number or letter, whether it is a letter, whether it is a number, whether it is blank, uppercase, lowercase

Python base 5--string

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.