Updated August 19, 2023
Introduction to Python String Functions
Various string methods such as center(), count(), find(), format(), index(), isalnum(), lower(), maketrans(), replace()etc. are incorporated by Python environment, and that are provided as in-built tools to the user, so that the user can manipulate the text data, perform requisite operations over the string, in the context of text analysis, based on the requirement, are termed as Python string functions.
Datatypes of Python String Methods
Python consists of five main data types, namely below:
- Numeric (sub-types of which are – int, long, float, and complex)
- String
- List
- Tuple
- Dictionary
For Example:
Code:
str1 = 'Hello from EduCBA'
OR
Str2 = "Hello from EduCBA"
This article will take a closer look at the String data type and its functions. Single and double quotes are used when assigning a string value to a variable.
Python String Functions and methods
The following are the Python string function mentioned:
1. capitalize(): Converts the initial letter of the string to uppercase.
Example:
str1 = "hello from EduCBA"
str2 = str1.capitalize()
print(str2)
Output:
2. casefold(): Converts the entire string to lowercase.
Example:
str1 = "HELLO FROM EduCBA"
str2 = str1.casefold()
print(str2)
Output:
3. center(): It aligns the string at the center of the specified length.
Example:
str1 = "EduCBA"
str2 = str1.center(10)
print(str2)
Output:
That is, two-character spaces on the left have left a void, followed by six characters of the string and another two void characters on the right, summing to a total of ten characters.
4. count(): Returns the number of times a substring occurs in the given string.
Example:
str1 = "Hello from EduCBA. Welcome to EduCBA"
num = str1.count("EduCBA")
print(num)
Output:
5. encode(): Converts the string into its encoded version.
Example:
str1 = "EduCBA"
str2 = str1.encode()
print("Hello from", str2)
Output:
6. endswith(): Returns true if the given string ends with the specified substring.
Example:
str1 = "Hello from EduCBA"
str2 = str1.endswith("CBA")
print(str2)
Output:
7. expandtabs(): Replaces the tab size with the given numeric character spaces. The default tab size is 8 character spaces.
Example:
str1 = "Hello\tfrom\tEduCBA"
str2 = str1.expandtabs(2)
print(str2)
Output:
8. find(): Searches the main string from the left for a specified substring and returns its position within a match is found; if not, return -1 when no match is found.
Example:
str1 = "Hello from EduCBA"
str2 = str1.find("EduCBA")
print(str2)
Output:
9. format(): Helps format the string by using placeholders.
Example:
str1 = "EduCBA"
print("Hello from {}.".format(str1))
Output:
10. index(): Finds the position of occurrence of a substring by searching the main string for a specified substring and returns its position within a match is found; if not, throws an error.
Example:
str1 = "Hello from EduCBA"
str2 = str1.index("EduCBA")
print(str2)
Output:
11. isalnum(): Determines if all the characters in a given string are alphanumeric, only alphabets and numbers. If yes, then returns true; else, returns false. In case there is a space in between, it returns false.
Example:
str1 = "EduCBA123"
str2 = str1.isalnum()
print(str2)
Output:
12. isalpha(): Determines if all the characters in the given string are alphabets. If yes, return true; else, return false. In case there is a space in between, it returns false.
Example:
str1 = "HellofromEduCBA"
str2 = str1.isalpha()
print(str2)
Output:
13. isdecimal(): Determines if all the characters in a given string are decimals. If yes, then returns true; else, returns false. In case there is a space in between, it returns false.
Example:
str1 = "123456"
str2 = str1.isdecimal()
print(str2)
Output:
14. isidentifier(): Determines whether or not the string is a valid identifier. If yes, then returns true; else, returns false. In case there is a space in between, it returns false.
Example 1:
str1 = "EduCBA123"
str2 = str1.isidentifier()
print(str2)
Output:
Example 2:
str1 = "EduCBA 123"
str2 = str1.isidentifier()
print(str2)
Output:
15. islower(): Determines if all the characters in a given string are lowercase. If yes, then returns true; else, returns false.
Example:
str1 = "EduCBA"
str2 = str1.islower()
print(str2)
Output:
16. isnumeric(): Determines if all the characters in a given string are numeric, that is, numbers and exponents that could be in fractions. If yes, then returns true; else, return false.
Example:
str1 = "123"
str2 = str1.isnumeric()
print(str2)
Output:
17. isprintable(): Determines if all the characters in a given string are printable or not. If yes, then returns true; else, returns false. Characters such as “\t” or “\n” are not printable.
Example 1:
str1 = "EduCBA123"
str2 = str1.isprintable()
print(str2)
Output:
Example 2:
str1 = "\tEduCBA123"
str2 = str1.isprintable()
print(str2)
Output:
18. isspace(): Determines if all the characters in a given string are white spaces. If yes, then returns true; else, returns false.
Example:
str1 = " "
str2 = str1.isspace ()
print(str2)
Output:
19. istitle(): Determines if a string follows a set of rules to qualify as a title. If yes, then returns true; else, returns false.
Example:
str1 = "Hello From Educba"
str2 = str1.istitle()
print(str2)
Output:
20. isupper(): Determines if all the characters in a given string are in the upper case. If yes, then returns true; else, returns false.
Example:
str1 = "HELLO FROM EDUCBA"
str2 = str1.isupper()
print(str2)
Output:
21. join(): Meant to concatenate two strings in an iterated manner.
Example:
str1 = "Hello"
str2 = str1.join("EduCBA")
print(str2)
Output:
22. lower(): Meant to convert the entire string to lowercase.
Example:
str1 = "Hello from EduCBA."
str2 = str1.lower()
print(str2)
Output:
23. upper(): Meant to convert the entire string to the upper case.
Example:
str1 = "Hello from EduCBA"
str2 = str1.upper()
print(str2)
Output:
24. replace(): Meant to replace a substring with another.
Example:
str1 = "Hello from EduCBA!"
str2 = str1.replace(" from"," there")
print(str2)
Output:
Conclusion
These are a few of the important string functions that are commonly used. These methods make developing code when working with strings easier and faster.
Recommended Articles
We hope that this EDUCBA information on “Python String Functions” was beneficial to you. You can view EDUCBA’s recommended articles for more information.