15 string methods necessary to learn Python, have you learned it?

How are you guys learning Python? String learned?

Strings are the most basic data type in Python. They are found in all Python programs. If you are using Python, you will use it.

So I summarized the 15 most important built-in string methods to share with you, hoping that you can find helpful skills for yourself.

1、isalnum()

Returns True if there is at least one character in the string and all characters are letters or numbers, otherwise returns False.

s = 'python'
print(s.isalnum())
# True

s = '123'
print(s.isalnum())
# True

s = 'python123'
print(s.isalnum())
# True

s = 'python-123'
print(s.isalnum())
# False

2、find()

Check whether the specified content is contained in the string, if it is, return the starting index value, otherwise return -1.

s = 'Machine Learning'
idx = s.find('a')

print(idx)
print(s[idx:])
# 1# achine Learning

s = 'Machine Learning'
idx = s.find('aa')

print(idx)
print(s[idx:])
# -1# g

Additionally, you can specify a starting range.

s = 'Machine Learning'
idx = s.find('a', 2)

print(idx)
print(s[idx:])
# 10
# arning

3、count()

Returns the number of times the specified content appears in a string.

n = 'hello world'.count('o')
print(n)
# 2

n = 'hello world'.count('oo')
print(n)
# 0

4、join()

string.join(seq). Combines all elements (string representations) in seq into a new string with string as the delimiter.

list_of_strings = ['string', 'methods', 'in', 'python']
s = '-'.join(list_of_strings)

print(s)
# string-methods-in-python

list_of_strings = ['string', 'methods', 'in', 'python']
s = ' '.join(list_of_strings)

print(s)
# string methods in python

5 rs rsplit ()

Delimit the string starting from the right

s = 'string methods in python'.rsplit(' ', maxsplit=1)

print(s)
# ['string methods in', 'python']

6、Slicing

slicing slices, taking some elements from a list or tuple according to certain conditions (such as a specific range, index, split value)

s = '   hello   '
s = s[:]

print(s)
#    hello

s = '   hello   '
s = s[3:8]

print(s)
# hello

7、strip()

The strip() method is used to remove the specified character (default is space or newline) or character sequence at the beginning and end of the string.

s = '   hello   '.strip()

print(s)
# hello

s = '###hello###'.strip()

print(s)
# ###hello###

When using the strip() method, spaces or newlines are removed by default, so the # sign is not removed.

You can add specified characters to the strip() method as shown below.

s = '###hello###'.strip('#')

print(s)
# hello

In addition, when the specified content is not at the head and tail, it will not be removed.

s = ' \n \t hello\n'.strip('\n')

print(s)
#
#      hello

s = '\n \t hello\n'.strip('\n')

print(s)
#      hello

There is a space before the first \n, so only the trailing newline will be taken.

Finally, the parameter of the strip() method is to strip all combinations of its values, which can be seen in the following case.

s = 'www.baidu.com'.strip('cmow.')

print(s)
# baidu

The outermost first and last character parameter values ​​will be stripped from the string. Characters are removed from the front end until a string character that is not contained in the character set is reached.

A similar action occurs at the tail

8、lstrip()

Removes the specified character (space or newline by default) or sequence of characters from the left side of the string.

s = '   hello   '.lstrip()

print(s)
# hello

Likewise, all strings on the left that are contained in the character set can be removed.

s = 'Arthur: three!'.lstrip('Arthur: ')

print(s)
# ee!

9、rstrip()

Removes the specified character (space or newline by default) or sequence of characters from the right side of the string.

s = '   hello   '.rstrip()

print(s)
#    hello

10、removeprefix()

The function to remove the prefix in Python 3.9.

# python 3.9
s = 'Arthur: three!'.removeprefix('Arthur: ')

print(s)
# three!

Compared with strip(), the strings in the character set are not matched one by one.

11、removesuffix()

The function to remove the suffix in Python 3.9.

s = 'HelloPython'.removesuffix('Python')

print(s)
# Hello

12、replace()

Replace the content of the string with the specified content.

s = 'string methods in python'.replace(' ', '-')

print(s)
# string-methods-in-python

s = 'string methods in python'.replace(' ', '')

print(s)
# stringmethodsinpython

13、re.sub()

re is a regular expression, and sub is a substitute for substitution.

re.sub is a relatively complex replacement.

import re
s = "string    methods in python"
s2 = s.replace(' ', '-')
print(s2)
# string----methods-in-python

s = "string    methods in python"
s2 = re.sub("\s+", "-", s)
print(s2)
# string-methods-in-python

Compared with replace(), using re.sub() for replacement is indeed more advanced.

14、split()

The string is delimited, and the final result is a list.

s = 'string methods in python'.split()

print(s)
# ['string', 'methods', 'in', 'python']

When no delimiter is specified, it is separated by spaces by default.

s = 'string methods in python'.split(',')

print(s)
# ['string methods in python']

15、upper()

Convert all letters in a string to lowercase.

s = 'SIMPLE IS BETTER THAN COMPLEX'.lower()

print(s)
# simple is better than complex

Brothers, that's all for today's sharing. Have you all lost your studies?

It's hard to learn, it's hard to persevere! Share with you! come on!

insert image description here

Guess you like

Origin blog.csdn.net/fei347795790/article/details/123283383