Use of built-in string methods and built-in string Methods

Source: Internet
Author: User
Tags string methods

Use of built-in string methods and built-in string Methods
Use of built-in string Methods

Set string: st = 'Hello kitty'

1. count the number of elements

Print (st. count ('l') # The result is 2.

2. uppercase letters

Print (st. capitalize () # The result is Hello kitty.

3. Center

Print (st. center (20, '#') # The result is #### hello kitty #####. Parameter 20 is the total number of output characters. Parameter '#' is used to supplement less than 20 characters after the st string is removed with '#', and the st string is centered.

4. Determine whether it starts with a specific content

Print (st. startswith ('hes') # The result is true. Determines whether the string st starts with 'he. Yes, 'true' is output; otherwise, 'false' is output'

5. Search for the index value of an element in the string.

Print (st. find ('T') # The result is 8. Search for the index value that appears in the string from left to right. This method can only find one element at a time.

Print (st. rfind ('T') # The result is 9. Search for the index value that appears in the string from right to left. This method can only find one element at a time.

6. format the output

St1 = '{name} is {age}' # defines the string st1

Print (st1.format (ame = 'Tom ', age = 3) # result: tom is 3

Print (st1.format _ map ({'name': 'Tom ', 'age': 3) # result: tom is 3

7. Output in lower case

Print ('My tittle'. lower () # The result is my tittle. Converts all uppercase letters in a string to lowercase letters for output.

8. Output in lower case

Print ('My tittle'. upper () # The result is my tittle. Converts all lowercase letters in the string to uppercase and lowercase letters.

9. Remove character string spaces, line breaks, and tabs

Print ('\ n my tittle \ n'. strip () # The result is my tittle. Remove spaces, line breaks, and tabs at the beginning and end.

Print ('\ n my tittle \ n'. lstrip () # The result is my tittle. Remove leading spaces, line breaks, and tabs

Print ('\ n my tittle \ n'. rstrip () # The result is my tittle. Remove trailing spaces, line breaks, and tabs

10. Replacement

Print ('My tittle tittle'. replace ('tittle', 'leson', 2) # The result is my lesson. The 'tittle' parameter is the character to be replaced. The parameter 'leson' is a modified character. The parameter '2' is the number of modifications.

11. Split output

Print ('My tittle'. split ('') # The result is ['my, 'tittle', 'tittle']. Split the string with spaces. It can also be separated by other symbols, but must be within the string. You only need to change the split parameter. You can also set the number of splits in the second parameter of split.

Print ('My tittle'. rsplit ('', 1) # The result is ['my tittle', 'tittle']. Different from split, split starts from right, while split starts from left. The rest are the same.

12. Judge to end with a string.

Print (st. endswith ('y') # The result is true. True is output if it is correct, and false is output if it is incorrect.

13. Determine whether a string contains special characters.

Print ('as # dk '. isalnum) # The result is false. Because there are '#' special characters.

Print ('Kang Kang dh223'. isalnum) # The result is true.

14. Determine if it is a number

Print ('200'. isdigit () # The result is true. The error is false.

15. determine whether to use decimal

Print ('200'. isdecimal () # The result is true. The error is false.

16. Determine whether each letter is capitalized.

Print ('My tittle'. istittle () # The result is true. The error is false.

Print ('My tittle'. istittle () # The result is false.

17. Determine whether all strings are in uppercase

Print ('sdk'. isupper () # The result is true. The error is false.

18. Determine whether all strings are in lowercase.

Print ('hfjhs '. islower () # The result is true. The error is false.

19. Determine whether it starts with a space.

Print ('edkj '. isspace () # The result is true. The error is false.

20. Change uppercase to lowercase and output to uppercase.

Print ('sdhhd'. swapcase () # The result is sdhHD.

21. Add character 1) join

A = 'asd'

B = '000000'

C = ''. join ([a, B]) # concatenate a and B to obtain c

Print (c) # The result is asd123.

2) ljust

Print ('asd '. ljust (8,' * ') # The result is asd *****

3) must UST

Print ('asd '. Must ust (8,' * ') # The result is ***** asd.

Related Article

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.