Python basic learning 9 (string)

Posted by mitchell on Thu, 23 Dec 2021 14:53:24 +0100

catalogue

preface

1, String judgment

2, String replacement and merging

3, String comparison

4, Slicing of strings

preface

Continue the operations related to strings in the previous article. The operations in this article include: String judgment, string replacement and merging, string comparison and string slicing

1, String judgment

Method for judging string:

isidentifier(): judge whether the specified string is a legal identifier

isspace(): judge whether the specified string consists of all blank characters (carriage return, line feed, horizontal tab)

isalpha(): determines whether the specified string is composed of all letters

isdecimal(): judge whether the specified string is all composed of decimal numbers

isnumeric(): judge whether the specified string is all composed of numbers

isalnum(): judge whether the specified string is composed of letters and numbers

The demonstration code is as follows:

print('1', 'hello,world'.isidentifier()) # False
print('2', 'hello'.isidentifier()) # True
print('3', 'Zhang San'.isidentifier()) # True
print('4', 'Zhang San_123'.isidentifier()) # True

print('5', '\t'.isspace()) # True

print('6', 'abc'.isalpha()) # True
print('7', 'Zhang San'.isalpha()) # True
print('8', 'Zhang San 1'.isalpha()) # False

print('9', '123'.isdecimal()) # True
print('10', '123 four'.isdecimal()) # False
print('11', 'ⅰⅱⅲ'.isdecimal()) # False

print('12', '123'.isnumeric()) # True
print('13', '123 four'.isnumeric()) # True
print('14', 'ⅰⅱⅲ'.isnumeric()) # True

print('15', 'abc1'.isalnum()) # True
print('16', 'Zhang San 5'.isalnum()) # True
print('17', 'zhangsan!'.isalnum()) # False

2, String replacement and merging

String replacement:

replace(): the first parameter specifies the substring to be replaced, and the second parameter specifies the string to replace the substring. This method {returns the string obtained after replacement. The string before replacement does not change. When calling this method, you can specify the maximum number of replacements through the third} parameter

Merge of strings:

join(): combines strings from a list or tuple into one string

The demonstration code is as follows:

# replace
a = 'hello java'
print(a.replace('java', 'python'))
b = 'hello java java java java'
print(b.replace('java', 'python', 2))

# merge
lst = ['hello','java','python']
print('|'.join(lst))
print(' '.join(lst))

t = ('hello', 'java', 'python')
print(' '.join(t))

print('*'.join('python'))

The results are as follows:

3, String comparison

String comparison operation

Operators: >, > =, <, < =, ==

Comparison rule: first compare the first character in the two strings. If they are equal, continue to compare the next character, and then compare them successively. When the characters in the two strings are not equal, the comparison result is the comparison result of the two strings, All subsequent characters in the two strings will no longer be compared

Comparison principle: when comparing two characters, Compare the ordinal value (original value). Call the built-in function ord to get the ordinal value of the specified character. The built-in function chr corresponds to the built-in function ord. When calling the built-in function chr, specify the ordinal value to get the corresponding character

The demonstration code is as follows:

print('apple' > 'app')
print('apple' > 'banana')
print(ord('a'), ord('b'))
print(ord('temperature'), ord('Hua'))
print(chr(97), chr(98))
print(chr(28201), chr(21326))

result:

4, Slicing of strings

Slicing of strings:

String is an immutable type: it does not have operations such as adding, deleting and modifying; The slice operation produces a new object

The demonstration code is as follows:

c = 'hello,python'
c1 = c[:5]
c2 = c[6:]
c3 = '!'
newstr = c1 + c3 +c2
print(c1, id(c1))
print(c2, id(c2))
print(newstr, id(newstr))

result:

summary

The above eight items (including four items in string (2)) are basically all the operations of string. It is not very difficult. It is mainly to be proficient and practice more.

Topics: Python