preamble
Python strings are a built-in sequence of types. Strings can be used to work with textual data in Python.Python strings are immutable sequences of Unicode points. Creating strings in Python is the easiest to use. To create a string 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 will discuss some important and useful string functions in Python for data analysis and data manipulation, mainly for Natural Language Processing (NLP).
The Python string functions we will discuss in this article are as follows:
I. capitalize() function
The capitalize() function returns a string where the first character is capitalized.
Grammar:()
Example 1: Capitalizing the first letter of a given sentence
string = "CSDN is the largest developer community in China" print(())
Output:
Csdn is the largest developer community in china
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(())
Output:
3th csdn force plan activities are very good
The lower( ) function
The lower() function returns a string where all characters in the given string are lowercase. This function does nothing with symbols and numbers, i.e., it just ignores them.
Syntax: ()
Example 1: Lowercase a Given String
string = "Haiyong is an excellent CSDN blogger" print(())
Output:
haiyong is an excellent csdn blogger
Example 2: What happens if there are numbers instead of characters?
string = '3th CSDN force plan activities are very good' print(())
Output:
3th csdn force plan activities are very good
Title( ) function
The title() function returns a string where the first character of each word in the string is capitalized. It is like a title or headline.
If any word in the string contains a number or symbol, this function converts the first letter thereafter to uppercase.
Syntax: ()
Example 1: Capitalizing the first letter of each word
string = "The blog you are reading will be on the hot list" print(())
Output:
The Blog You Are Reading Will Be On The Hot List
Example 2: What happens if there are numbers instead of characters?
string = '10 useful Python string functions you must know' print(())
Output:
10 Useful Python String Functions You Must Know
IV. 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, and both use casefold() for the conversion function.
Syntax: ()
Example 1: Making a given string lowercase
string = "CSDN is the largest developer community in China" print(())
Output:
csdn is the largest developer community in china
Example 2: What happens if there are numbers instead of characters?
string = '10 useful Python string functions you must know' print(())
Output:
10 useful python string functions you must know
V. The upper( ) function
The upper() function returns a string where all characters in the given string are capitalized. This function does nothing with symbols and numbers, i.e., it just ignores them.
Syntax: ()
Example 1: Capitalization of a given string
string = "CSDN is the largest developer community in China" print(())
Output:
CSDN IS THE LARGEST DEVELOPER COMMUNITY IN CHINA
Example 2: What happens if there are numbers instead of characters?
string = '10 useful Python string functions you must know' print(())
Output:
10 USEFUL PYTHON STRING FUNCTIONS YOU MUST KNOW
The count( ) function
The count() function finds the number of times the specified value (given by the user) appears in the given string.
Syntax: string .count( value, start, end )
Example 1: Return the number of times the value "CSDN" appears in the string.
string = "CSDN is the largest developer community in China" print(("CSDN "))
Output:
1
Example 2: Returns the number of times the value "CSDN" appears in the string from position 8 to 16.
string = "CSDN is the largest developer community in China" print(("analytics", 8, 16))
Output:
0
The find( ) function
The find() function looks for the first occurrence of the specified value. If the value is not found in the string, it returns -1.
The find() function is almost identical to the index() function, but the only difference is that the index() function throws an exception if it can't find a value.
Syntax: (value, start, end)
Example 1: Where does the letter "d" first appear in the text?
string = "CSDN is the largest developer community in China" print(("d"))
Output:
20
Example 2: When searching only between positions 5 and 16, where in the text does the letter "d" first appear?
string = "CSDN is the largest developer community in China" print(("d", 12, 22))
Output:
20
Example 3: If the value is not found, the find() function returns -1, but the index() function throws an exception
string = "CSDN is the largest developer community in China" print(("d", 5, 10))
Output:
-1
Eight, replace () function
The replace() function replaces the specified phrase with another specified phrase.
Note: If nothing else is specified, all occurrences of the specified phrase will be replaced.
grammatical: string .replace( oldvalue, newvalue, count )
Example 1: Replace all occurrences of the word "developer " with the word "developer".
string = "CSDN is the largest developer community in China" print(("largest ", "best "))
Output:
CSDN is the best developer community in China
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(("developer ", "developers ", 1))
Output:
CSDN is China's largest developers community suitabsle for developer to learn
IX. swapcase( ) function
The swapcase() function returns a string where all uppercase letters are lowercase and vice versa.
Syntax: ()
Example 1: Changing Lowercase Letters to Uppercase and Uppercase Letters to Lowercase
string = "CSDN is the largest developer community in China" print(())
Output:
csdn IS THE LARGEST DEVELOPER COMMUNITY IN cHINA
Example 2: What happens if there are numbers instead of characters?
string = '3th CSDN force plan activities are very good' print(())
Output:
3TH csdn FORCE PLAN ACTIVITIES ARE VERY GOOD
X. join () function
The join() function takes all the items in an iterable object and joins them into a string. We must specify a string as the separator.
Syntax: (iterable)
Example 1: Concatenate all items in a given tuple into a string, using the # (hashtag) character as a separator.
myTuple = ("Computer Scientist", "Programming Learning", "Python Programming") x = " # ".join(myTuple) print(x)
Output:
Computer Scientist # Programming Learning # Python Programming
Example 2: Concatenate all the items in a given dictionary into a single string, using the word "TEST" as the separator.
myDict = {"name": "CSDN", "country": "China", "Technology": "Python Programming"} mySeparator = "TEST" x = (myDict) print(x)
Output:
nameTESTcountryTESTTechnology
to this article on the 10 useful Python string function summary of the article is introduced to this, more related Python string function content, please search my previous posts or continue to browse the following related articles I hope you will support me more in the future!