This is the 11th day of my participation in the August More text Challenge. For details, see: August More Text Challenge
👓 preface
Python strings are a built-in sequence of types. Strings can be used to process textual data in Python. Python strings are immutable sequences of Unicode points. Creating strings in Python is easiest to use. To create strings in Python, we simply enclose the text in single and double quotes. Python treats single and double quoted statements the same way. So in this article, we’ll discuss some important and useful string functions in Python for data analysis and data manipulation, primarily for natural language processing (NLP).
The Python string functions we’ll discuss in this article are:
😊 capitalize() function
The capitalize() function returns a string in which the first character is capitalized.
Grammar: string. Capitalize ()
Example 1: Capitalize the first letter in a given sentence
string = "CSDN is the largest developer community in China"
print(string.capitalize())
Copy the code
Output:
Csdn is the largest developer community in china
Copy the code
Example 2: What happens if the first character is a number instead of a character
string = '3th CSDN force plan activities are very good'
print(string.capitalize())
Copy the code
Output:
3th csdn force plan activities are very good
Copy the code
💌 2, lower() function
The lower() function returns a string in which all characters in a given string are lowercase. This function has nothing to do with symbols and numbers, that is, it just ignores these things.
Grammar: string. The lower ()
Example 1: Lowercase given string
string = "Haiyong is an excellent CSDN blogger"
print(string.lower())
Copy the code
Output:
haiyong is an excellent csdn blogger
Copy the code
Example 2: What happens if you have numbers instead of characters
string = '3th CSDN force plan activities are very good'
print(string.lower())
Copy the code
Output:
3th csdn force plan activities are very good
Copy the code
⏰ 3. Title () function
The title() function returns a string in which the first character of each word in the string is uppercase. It’s like a title or a title.
If any word in the string contains a number or symbol, this function converts the first letter following it to uppercase.
Grammar: string. Title ()
Example 1: Capitalize the first letter of each word
string = "The blog you are reading will be on the hot list"
print(string.title())
Copy the code
Output:
The Blog You Are Reading Will Be On The Hot List
Copy the code
Example 2: What happens if you have numbers instead of characters
string = '10 useful Python string functions you must know'
print(string.title())
Copy the code
Output:
10 Useful Python String Functions You Must Know
Copy the code
🧿 4. casefold() function
The casefold() function returns a string in which all characters are lowercase.
This function is similar to the lower() function, but the casefold() function is more powerful and aggressive, meaning that it converts more characters to lowercase and finds more matches when comparing two strings, both using casefold() for conversion.
Grammar: string casefold ()
Example 1: Lowercase a given string
string = "CSDN is the largest developer community in China"
print(string.casefold())
Copy the code
Output:
csdn is the largest developer community in china
Copy the code
Example 2: What happens if you have numbers instead of characters
string = '10 useful Python string functions you must know'
print(string.casefold())
Copy the code
Output:
10 useful python string functions you must know
Copy the code
🏀 5. Upper () function
The upper() function returns a string in which all characters in the given string are uppercase. This function has nothing to do with symbols and numbers, that is, it just ignores these things.
Grammar: string. Upper ()
Example 1: Uppercase of a given string
string = "CSDN is the largest developer community in China"
print(string.upper())
Copy the code
Output:
CSDN IS THE LARGEST DEVELOPER COMMUNITY IN CHINA
Copy the code
Example 2: What happens if you have numbers instead of characters
string = '10 useful Python string functions you must know'
print(string.upper())
Copy the code
Output:
10 USEFUL PYTHON STRING FUNCTIONS YOU MUST KNOW
Copy the code
🏆 6. Count () function
The count() function looks for the number of times a specified value (given by the user) appears in a given string.
Syntax: string.count (value, start, end)
Example 1: The number of times the return value “CSDN” appears in a string
string = "CSDN is the largest developer community in China"
print(string.count("CSDN "))
Copy the code
Output:
1
Copy the code
Example 2: The number of times the return value “CSDN” appears in the string from positions 8 to 16
string = "CSDN is the largest developer community in China"
print(string.count("analytics".8.16))
Copy the code
Output:
0
Copy the code
🎻 7. Find () function
The find() function looks for the first occurrence of a specified value. If the value is not found in the string, -1 is returned.
The find() function is almost identical to the index() function, but the only difference is that the index() function throws an exception when no value is found.
Syntax: string.find(value, start, end)
Example 1: Where does the letter “D” first appear in text?
string = "CSDN is the largest developer community in China"
print(string.find("d"))
Copy the code
Output:
20
Copy the code
Example 2: Where in the text does the letter “d” first appear when searching only between positions 5 and 16?
string = "CSDN is the largest developer community in China"
print(string.find("d".12.22))
Copy the code
Output:
20
Copy the code
Example 3: If the value cannot be found, the find() function returns -1, but the index() function throws an exception
string = "CSDN is the largest developer community in China"
print(string.find("d".5.10))
Copy the code
Output:
-1
Copy the code
🎥 8. Replace () function
The replace() function replaces a specified phrase with another specified phrase.
Note: If nothing else is specified, all occurrences of the specified phrase will be replaced.
Syntax: string.replace (oldvalue, newvalue, count)
Example 1: Replace all occurrences of the word “developer”
string = "CSDN is the largest developer community in China"
print(string.replace("largest "."best "))
Copy the code
Output:
CSDN is the best developer community in China
Copy the code
Example 2: Replace only the first occurrence of the word “developer”
string = "CSDN is China's largest developer community suitabsle for developer to learn"
print(string.replace("developer "."developers ".1))
Copy the code
Output:
CSDN is China's largest developers community suitabsle for developer to learn
Copy the code
🍖 9. Swapcase () function
The swapCase () function returns a string in which all uppercase letters are lowercase letters and vice versa.
Grammar: string swapcase ()
Example 1: Change lowercase letters to uppercase and uppercase letters to lowercase
string = "CSDN is the largest developer community in China"
print(string.swapcase())
Copy the code
Output:
csdn IS THE LARGEST DEVELOPER COMMUNITY IN cHINA
Copy the code
Example 2: What happens if you have numbers instead of characters
string = '3th CSDN force plan activities are very good'
print(string.swapcase())
Copy the code
Output:
3TH csdn FORCE PLAN ACTIVITIES ARE VERY GOOD
Copy the code
✨ 10. Join () function
The join() function takes all the items in the iterable and concatenates them into a string. We must specify a string as the delimiter.
Grammar: string. Join (iterable)
Example 1: Concatenate all the items in a given tuple into a string, using the # (hashtag) character as the delimiter
myTuple = ("Computer Scientist"."Programming Learning"."Python Programming")
x = "#".join(myTuple)
print(x)
Copy the code
Output:
Computer Scientist # Programming Learning # Python Programming
Copy the code
Example 2: Concatenate all items in a given dictionary into a string, using the word “TEST” as the delimiter
myDict = {"name": "CSDN"."country": "China"."Technology": "Python Programming"}
mySeparator = "TEST"
x = mySeparator.join(myDict)
print(x)
Copy the code
Output:
nameTESTcountryTESTTechnology
Copy the code
😊 end of want to say
I hope you enjoyed this article. If you like it, share it with your friends. Feel free to comment below and I’ll get back to you as soon as possible. 😉
I’ve been writing a technical blog for a long time, mostly through Nuggets, and this is my python basics tutorial. I like to share technology and happiness through articles. For more information, you can visit my blog: Gold Diggers – Sea People. Hope you like it!
If you do learn something new from this post, like it, bookmark it and share it with your friends. 🤗 Finally, don’t forget ❤ or 📑 for support