String correlation method

1.center,rjust,ljust,zfill

String. Center (length, padding character)

String. Rjust (length, padding character)

String. Ljust (length, padding character)

String. Zfill (length)

str1='ab'
print(str1.center(9,'1')) # 11abc111
print(str1.rjust(10,'%')) # %%%%%%%%ab
print(str1.zfill(4)) # 00ab

# When the length is less than the string length, the original string is output
print(str1.zfill(1)) # ab
print('abc'.rjust(2,'#')) # abc
print('abc'.ljust(2,'#')) # abc

2.count

String 1. Count (string 2) - counts the number of occurrences of string 2 in string 1

String 1. Count (string 2, start subscript, end subscript) ---- the range can be specified, and the following table can be ended at the start subscript ~ end subscript

str1='ab'
print(str1.count('ab')) # 1
message='you see see , one day day'
print(message.count('a')) # 2
print(message.count('d',-7,-2)) # 2
print(message.count('d',-4)) # 1

3.find and index ---- find rfind and rindex from right to left

# String 1. Find (string 2) -- the position where string 2 first appears in string 1
# String 1. Index (string 2) -- the position where string 2 first appears in string 1
message='you see see , one day day'
print(message.find('see')) # 4
print(message.index('see')) # 4
# # When the string 2 is not found, it returns different, find returns - 1, and index reports an error
print(message.find('abc')) # -1
# print(message.index('abc')) # ValueError: substring not found
print(message.rfind('see')) # 8
print(message.rindex('see')) # 8
print(message.rfind('are')) # -1

4.isalnum,isalpha,isdigit,isnumeric,isspace,isupper,islower

a. String. isalnum() --- judge whether the string contains only numbers, letters and Chinese
b. String. isalpha() -- judge whether the string contains only letters and Chinese
c. String. isdigit() - determines whether the string is a pure numeric string (numeric character)
d. String. isnumeric -- judge whether the string is a pure numeric string (the character of a number in meaning)
e. String. isspace() -- judge whether the string is a blank character
f. String. isupper() -- judge whether all letters in the string are uppercase letters
g. String. islower() -- judge whether all letters in the string are lowercase letters

All return values are Boolean

5. String. Join (sequence) -- merge the elements in the sequence into a new string through the specified string (all elements in the sequence must be strings)

# Exercise: you have created a list. Use '= =' to connect all strings in the list into a new string
list1 = [100, 'abc', True, 'Hello', 'hello', 12.9]     # 'abc = = hello = = hello'
result='=='.join([x for x in list1 if type(x)==str])
print(result)

6.strip,lstrip,rstrip -- remove whitespace on both sides of the string

main_str ='    abc123   '
print(main_str,';',sep='')# '    abc123   ;'
print(main_str.strip(),';',sep='') # 'abc123;'
print(main_str.lstrip(),';',sep='')# 'abc123   ;'
print(main_str.rstrip(),';',sep='') # '    abc123;'

7.maketrans,translate

Str.maketrans (string 1, string 2) -- create the corresponding table of string 1 and string 2

String. Translate (corresponding table) -- replace according to the corresponding table

message='Today's Wednesday, tomorrow's Thursday and Friday will be off, and you can go out on Saturday'
table=str.maketrans('0123456','123456')
result=message.translate(table)
print(result) # Today, Thursday, tomorrow, Friday, Saturday will be off, and you can go out on Sunday

8.split

String 1. Split (string 2) -- cut all strings 2 in string 1 as cutting points

result='abcand123andmn'.split('and')
print(result) # ['abc', '123', 'mn']

String 1. Split (string 2,N) -- take the first N string 2 as the cutting point

result='abcMN123MNllasdMN'.split('MN',2)
print(result) # ['abc', '123', 'llasdMN']

9.replace

String 1. Replace (string 2, string 3) - replaces string 2 in string 1 with string 3

message='abcMN123MNllasdMN'
print(message.replace('MN','Hello')) # abc Hello 123 Hello llasd hello

String 1. Replace (string 2, string 3,N) - replaces the first N string 2 in string 1 with string 3

message='abcMN123MNllasdMN'
print(message.replace('MN','Hello',2)) # abc Hello 123 Hello llasdMN

Tags: Python

Posted on Fri, 22 Oct 2021 07:16:30 -0400 by Megalink