String common operations

''' string method
s.index()       s.rindex()
s.find()        s.rfind()
s.count()
s.replace()
s.partition()   s.rpartition()
s.split()       s.rsplit()          s.splitlines()
s.join()
s.strip()       s.lstrip()          s.rstrip()

s.lower()       s.upper()           s.capitalize()      s.title()
s.swapcase()
s.center () s.ljust () s.rjust () s.zfill ()
s.startswith()  s.endswith()
s.isdigit () s.isalpha () s.istitle () s.isalnum ()
s.isspace()     s.isupper()         s.islower()         s.isnumeric()
s.isdecimal () s.isidentifier ()
s.isprintable ()
s.format()
s.encode()
s.translate()
s.expandtabs()
s.maketrans ()
'''


# create
s = 'asddd'
print(s)
s1 = str('Hello World!')
print(1, s1)
print(s.count('d')) # Returns the number of the given string in the monitored string

# Modify# The string cannot be modified, you have to change it and assign it to another variable, just use another variable
s2 = s1 [2]
print(2, s2)
s3 = s1.replace('l', 'L', 2) # replace(old string, new string, number of replacements) new replace old,
print(3, s3) # Only strings can be filled in, that is, with ' ', the length is not limited
s4 = s1.replace('q', 'Q', 2) # don't replace if the old string is not found
print(4, s4)

# slice
s5 = s1[::] # str[start position: end to end (exclusive): step size]
print(5, s5) # Intercept the string from the start position (0), the default step size is 1 and intercept all
s6 = s1[::-1] # Negative numbers mean backwards, and if the step size is negative, it is backwards, so the start position should be behind the end position, such as [5:1]
print(6, s6) # The negative number represents the penultimate position

# Increase
# You can directly use the + sign to realize the connection between strings
print(s1+'hello')
s7 = s1.zfill(15) # zfill(int) fills in the int type, indicating that 0 is used to fill the string with the length of this int
print(7, s7)
s8 = s1.ljust(15, '0') # ljust(width(int),str) is left-aligned and padded with the str string, so that the length of the string is enough for the specified int
print(8, s8)
s9 = s1.rjust(15, '0') # The same method as above is just right-aligned, center(width(int),shr) is centered and filled with left and right
print (9, s9)
s10 = s9.lstrip('0') # lstrip(str), delete the given str character on the left side of the string
s11 = 'p'.lstrip() # rstrip(str) is to delete the str string on the right, and strip is to delete the given string on both sides
print(10, s10) # defaults to clear spaces
print(11, s11)

# delete
del s11 # del delete, there are two ways to write, del (thing to delete) or del to delete something
## print(12, s11)

# operate
print(s1.capitalize()) # Convert the first letter of the string to uppercase, and other symbols with uppercase to lowercase
print(s1.title()) # Turn the first letter of the word encountered into uppercase, and all other letters into lowercase
s12 = s1.upper() # make all letters uppercase
print(13, s12)
s13 = s1.lower() # Make all letters lowercase
print(14, s13)
s14 = s1.swapcase() # Swap case of letters
print(15, s14)
s15 = ' '.join(s1) #a.join(b) put a string between each character of a string
print(16, s15)
n1 = s1.find('Wo', 0, len(s1))    # find(str, start, end)
print(n1) # find the string str, between the interval start and end
n2 = s1.find('WO', 0, len(s1)) # If found, return the index of the first position when found (from left to right)
print(n2) # return -1 if not found
n3 = s1.index('Wo', 0, len(s1))   # index(str, start, end)
print(n3) # find the string str, between the interval start and end
## n4 = s1.index('WO', 0, len(s1)) # The method found is the same as above, but if it is not found, it will not return -1, and an error will be reported
## print(n4)
l1 = s1.split(' ', 1)                # split(str, int)
print(l1) # Use the given string str (the original string exists) to split the string into a list, the default is a space
l2 = s1.splitlines() # The original string does not exist. The given str is not split, and it becomes a list directly
print(l2) # int Given the number of splits, the splitlines method has no parameters
                                     # splitlines splits the string into a list with a newline
s16 = s1.partition(' ') # partition(str) uses the given string (in the original string) to divide the string into three parts and put them into a tuple
print(17, s16) # For example hello world! --->('hello',' ','world!')
# There are also rfind, rindex, rsplit, rpartition methods as above, but the direction is from right to left

s17 = s1.startswith('ll', 2, 4) # Given a range, return true if the starting string is equal to the given string, otherwise return false
print(18, s17)
s18 = s1.endswith('or', -5, -3) # Same as above, but it becomes the end string
print(19, s18)

print(20, s1.isalpha()) # Determine whether it is a string composed of all letters, bool type
print(21, s1.isalnum()) # Returns whether it is a string of letters or numbers, bool
print(22, s1.isdigit()) # Returns whether it is a string composed of digits, bool
print(23, s1.isspace()) # Returns whether it is a string composed of spaces, bool
# There are also some judgment strings starting with is, as the name implies, they are all judged bool types
print(list(enumerate(s1)))
for i in enumerate(s1): # Convert the enumeration type, each character corresponds to the corresponding serial number (subscript) to form a tuple
    print(i, end=' ')

print()

for i, y in enumerate(s1): # Split the serial number (subscript) in the enum tuple and the corresponding character in the string
    print(i, end=' ')
    print(y)

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325900524&siteId=291194637