# coding:utf-8
if __name__ == '__main__':
''' Defined by single quotation marks Defined by three single quotation marks Define by three double quotation marks '''
a = 'this is string'
b = '''this is string'''
c = """this is string"""
print(a, b, c)
title case
# coding:utf-8
if __name__ == '__main__':
''' string.capitalize() Capitalize the initial of a string '''
a = 'i am xieruixiang'
print(a.capitalize()) # I am xieruixiang
All capitals
# coding:utf-8
if __name__ == '__main__':
''' string.upper() Capitalize all letters in the string '''
a = 'i am xieruixiang'
print(a.upper()) # I AM XIERUIXIANG
All letters lowercase
# coding:utf-8
if __name__ == '__main__':
''' string.lower(),string.casefold() All the letters in the string can be lowercased casefold yes lower The enhanced '''
a = 'I AM XIERUIXIANG'
print(a.lower()) # i am xieruixiang
print(a.casefold()) # i am xieruixiang
Reverse case
# coding:utf-8
if __name__ == '__main__':
''' string.swapcase() Can change uppercase to lowercase in a string , Lowercase to uppercase . Pole reversal '''
a = 'i AM xIErUIxIANG'
print(a.swapcase()) # I am XieRuiXiang
fill 0 To a specified length
# coding:utf-8
if __name__ == '__main__':
''' string.zfill(width) use 0 Fill the string to the specified width length , If the string length is greater than or equal to width It doesn't fill in '''
a = 'abcdef'
print(a.zfill(6)) # abcdef
print(a.zfill(10)) # 0000abcdef
Count the number of a member in the string
# coding:utf-8
if __name__ == '__main__':
''' string.count(item) There are several in the statistics string item '''
a = 'abaacdef'
print(a.count('a')) # 3
print(a.count('aa')) # 1
print(a.count('ha')) # 0
Whether the string starts or ends with a member
# coding:utf-8
if __name__ == '__main__':
''' string.startswith(item) Determine whether the string is based on item start string.endswith(item) Determine whether the string is based on item ending '''
a = 'abaacdef'
print(a.startswith('ab')) # True
print(a.endswith('ef')) # False
Find the position where the substring first appears in the main string
# coding:utf-8
if __name__ == '__main__':
''' string.find(item) and string.index(item) It's all about finding item First occurrence in string string.find() No return found -1 string.index() Return error not found So use string.find() more '''
a = 'abcedefg'
print(a.find('c')) # 2
print(a.index('c')) # 2
print(a.find('ac')) # -1
String filtering
# coding:utf-8
if __name__ == '__main__':
''' zstring.strip(char) Filter characters .char by None Then filter the characters of equivalent spaces , Otherwise filter char character , Filter one side from left to right , Filter again from right to left string.lstrip(char) and strip() identical , This function only filters from left to right string.rstrip(char) and strip() identical , This function only filters from right to left '''
a = ' abcdefgh '
b = ' abcdefgh '
c = 'aabbaa'
print(a.strip())
print(b.strip())
print(c.strip('a')) # bb
print(c.lstrip('a')) # bbaa
print(c.rstrip('a')) # aabb
String substitution
# coding:utf-8
if __name__ == '__main__':
''' string.replace(old,new,max) In the string old Replace the string with new character string , Replace max individual , When max by None Replace all when '''
a = 'abcdcdefg'
print(a.replace('cd', 'ff')) # abffffefg
print(a.replace('cd', 'ff', 1)) # abffcdefg
print(a.replace('hh', 'ff')) # abcdcdefg
A string of is function
# coding:utf-8
if __name__ == '__main__':
''' string.isspace() Determine whether the string is only composed of white space strings string.istitle() Determine whether the first letters of all words are capitalized string.isupper() Judge whether all English letters are capitalized string.islower() Judge whether all English letters are lowercase '''
a = ' a '
space = ' '
title = 'I Love You'
title2 = 'I Love you'
upper = 'I LOVE YOU'
upper2 = 'I LOVE You'
lower = 'i love you'
lower2 = 'I love you'
print(space.isspace()) # True
print(a.isspace()) # False
print(title.istitle()) # True
print(title2.istitle()) # False
print(upper.isupper()) # True
print(upper2.isupper()) # False
print(lower.islower()) # True
print(lower2.islower()) # False
String slice
# coding:utf-8
if __name__ == '__main__':
''' Slice using : 1.list[start:end:step] From the index start Start , every other step Get an element , Until index end( The retrieved value contains the index start Value on , Index not included end Value on ), Return to a new list start The default value is 0,end The default value is len(list),step The default value is 1 example 1 2. When step A negative number means to take... From right to left example 2 3. When start or end When it's negative , Means to confirm the index from right to left , The rightmost from the index -1 Start example 3 4. Because the string cannot be changed , So there is no slice assignment '''
a = 'abcdefg'
# example 1
print(a[::]) # abcdefg
print(a[0:len(a):1]) # abcdefg
# example 2
print(a[::-1]) # gfedcba
# example 3
print(a[-2:-1:1]) # f