欢迎访问宙启技术站
智能推送

10个Python函数实现常见字符串操作

发布时间:2023-06-14 05:31:10

1. split()

The split() function is a commonly used string operation that returns a list of substrings separated by a delimiter. For example, if we wanted to split a sentence into individual words, we could use the following code:

sentence = "This is a sample sentence."
words = sentence.split()
print(words)

Output:

['This', 'is', 'a', 'sample', 'sentence.']

2. strip()

The strip() function is used to remove any leading or trailing whitespace from a string. This is often used to clean up user input, as extra whitespace can cause unexpected behavior in code. For example:

text = "     Hello world!    "
clean_text = text.strip()
print(clean_text)

Output:

"Hello world!"

3. lower() and upper()

The lower() and upper() functions are used to convert all characters in a string to lowercase or uppercase, respectively. This can be useful for standardizing user input or comparing strings. For example:

text = "ThIs Is A TeSt"
lower_case = text.lower()
upper_case = text.upper()
print(lower_case) 
print(upper_case)

Output:

this is a test

THIS IS A TEST

4. replace()

The replace() function allows us to replace a substring within a string with another substring. This can be useful for data cleaning or formatting. For example:

string = "Hello, World!"
new_string = string.replace(",", ";")
print(new_string)

Output:

Hello; World!

5. join()

The join() function allows us to join a list of strings into a single string, using a specified delimiter. For example:

words = ["This", "is", "a", "test"]
string = " ".join(words)
print(string)

Output:

This is a test

6. find()

The find() function returns the index of the first occurrence of a substring within a string. If the substring is not found, it returns -1. For example:

string = "This is a test"
index = string.find("is")
print(index)

Output:

2

7. count()

The count() function returns the number of times a substring appears within a string. For example:

string = "Hello, World!"
count = string.count("l")
print(count)

Output:

3

8. startswith() and endswith()

The startswith() and endswith() functions are used to check whether a string starts or ends with a certain substring, respectively. These functions can be useful for data filtering or processing. For example:

file_name = "example.txt"
if file_name.endswith(".txt"):
    print("This is a text file.")

Output:

This is a text file.

9. title()

The title() function is used to capitalize the first letter of each word in a string. This can be useful for formatting titles or headings. For example:

string = "this is a test"
new_string = string.title()
print(new_string)

Output:

This Is A Test

10. isdigit()

The isdigit() function returns True if a string contains only digits (0-9), and False otherwise. This can be useful for data validation or processing. For example:

string = "123456"
if string.isdigit():
    print("This string contains only digits.")

Output:

This string contains only digits.