Understanding Python in one article (two)-string articles

This article focuses on the related methods of Python strings. There are many special methods, but there are also more peculiar methods, which may not be thought of, hahaha! I will post the results of the more difficult to understand, but I will not post the simple ones. Finally, I wish you all a happy learning!

1. Create a string

str1 = 'I love FishC.com'
str1

2. Change the first character of the string to uppercase --capitalize()

str2 = 'xiaoxie' 
str2.capitalize() # 该方法不会改变原字符串,返回一个新的字符串

3. Change all characters of the entire string to lowercase --casefold()

str3 = 'DAXIEDONGJINkun'
str3.casefold()

4. Return the number of occurrences of sub in the string, start and end indicate the range, optional –count(sub,[start],[end]])

str4 = 'xiaoxiexiaoxie'
str4.count('xi')

5. Check if the string ends with sub substring-endwith(sub,[[start],[end]])

str4.endswith('xie')

6. Use the string as the delimiter to insert between all the characters in sub -join(sub)

str5 = 'dongjinkun'
str5.join('123')

Insert picture description here

7. Convert all uppercase characters in the string to lowercase --lower()

str6 = 'DAXIE'
str6.lower()

8. Check whether sub is contained in the string, and if so, return the index value, otherwise return -1. The start and end parameters indicate the range, which is optional. --Find(sub,start,end)

str6 = 'I love kiki'
str6.find('k')

9. If the string has at least one character and all characters are letters, it returns True, otherwise it returns False–isalpha()

str7 = 'IloveChinaforever'
str7.isalpha()

10. If there is at least one character in the string and all characters are letters or numbers, it returns True, otherwise it returns False—isalnum()

str8 = '123455aaaa677'
str8.isalnum()

11. If there is at least one character in the string and all characters are numbers, it returns True, otherwise it returns False–isdigit()

str9 = '123444'
str9.isdigit()

12. If the string is titled, (all words start with uppercase, and the rest of the characters are lowercase) return True, otherwise return False–istitle()

str10 = 'Fishc'
str10.istitle()

13. Remove all spaces on the left side of the string -lstrip()

# 并不会改变原字符串
str11 = '    I love FichC    '
str11.lstrip()

14. Remove all spaces on the right side of the string --rstrip()

# 并不会改变原字符串
str11.rstrip()

15. Find the string sub, divide the original string into a tuple containing 3 elements (pre_sub, sub, post_sub), if the string does not contain sub, return (original string, ``,'')-partition (sub)

str12 = 'I love FichC.com'
str12.partition('ov')

Insert picture description here

16. Replace the old string in the string with the new string. If count is specified, the replacement will not exceed count times -replace(old,new,count)

str12.replace('ov','djk ov')

Insert picture description here

17. Without parameters, the default is to slice the string with spaces as the separator. If the maxsplit parameter is set, only maxsplit substrings are separated, and the list of substring splicing after slicing is returned –split(sep = None, maxsplit =- 1)

# 不带任何参数
str12.split()

Insert picture description here

# 设置分割符
str12.split('i')

Insert picture description here

18. Check whether the string starts with prefix, if yes, return True, otherwise return False. The start and end parameters can specify range checking, which is optional. --Starstwith(prefix,start,end)

str12.startswith('I ')

19. Delete all the spaces before and after the string, the chars parameter can specify the deleted characters, optional-strip (chars)

str13.zfill(50)

str13 = '    I love FishC.com     '
str13.strip()

20. Reverse case in a string-swapcase()

str13.swapcase()

21. Convert all lowercase characters in a string to uppercase --upper()

str13.upper()

22. Return the titled (all words start with uppercase, and the rest of the letters are lowercase) string –title()

str13.title()

23. Return a string of length width, the original string is right-aligned, and the front is filled with 0 -zfill(width)

str13.zfill(50)

Insert picture description here

Guess you like

Origin blog.csdn.net/dongjinkun/article/details/112676007