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 Functions
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
str1 = ‘Hello from EduCBA’
OR
Str2 = “Hello from EduCBA”
In this article, we will take a closer look at the String data type and its functions. Single and double quotes are made use of when assigning a string value to a variable.
Python String Functions
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:
Hello From EduCBA
2. casefold(): Converts the entire string to lowercase.
Example:
str1 = “HELLO FROM EduCBA”
str2 = str1.casefold()
print(str2)
Output:
hello from educba
3. center(): It aligns the string at the center of the specified length.
Example:
str1 = “EduCBA”
str2 = str1.center(10)
print(str2)
Output:
EduCBA
That is two character spaces on the left are left void followed by six characters of the string and another two void characters on the right, summing to a total of ten characters.
4.8 (7,864 ratings)
View Course
4. count(): Returns the number of times a substring occurs in the given string.
Example:
str1 = “Hello from EduCBA. Welcomw to EduCBA”
num = str1.count(“EduCBA”)
print(str2)
Output:
2
5. encode(): Converts the string into its encoded version.
Example:
str1 = “EduCBA”
str2 = str1.encode()
print(“Hello from”, str2)
Output:
Hello from vfg/7hyt/4
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:
true
7. expandtabs(): Replaces the tab size to 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:
Hello from EduCBA
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:
11
9. format(): Helps format the string by making use of placeholders.
Example:
str1 = “EduCBA”
print("Hello from {}.".format(str1))
Output:
Hello from EduCBA.
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
11. isalnum(): Determines if all the characters in a given string are alphanumeric that is 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:
True
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:
True
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:
True
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:
True
Example 2:
str1 = “EduCBA 123”
str2 = str1.isidentifier()
print(str2)
Output:
False
15. islower(): Determines if all the characters in a given string are in lower case. If yes then returns true, else returns false.
Example:
str1 = “EduCBA”
str2 = str1.islower()
print(str2)
Output:
False
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:
True
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: True
Example 2:
str1 = “\tEduCBA123”
str2 = str1.isprintable()
print(str2)
Output:
False
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:
True
19. istitle(): Determines if a string follows a set of rules in order to be qualified as a title. If yes then returns true, else returns false.
Example:
str1 = “Hello From Educba”
str2 = str1.istitle()
print(str2)
Output:
True
20. isupper(): Determines if all the characters in a given string are in upper case. If yes then returns true, else returns false.
Example:
str1 = “HELLO FROM EDUCBA”
str2 = str1.isupper()
print(str2)
Output:
True
21. join(): Meant to concatenate two strings in an iterated manner.
Example:
str1 = “Hello”
str2 = str1.join(“EduCBA”)
print(str2)
Output:
HelloEHellodHellouHelloCHelloBHelloA
22. lower(): Meant to convert the entire string to lower case.
Example:
str1 = “Hello from EduCBA.”
str2 = str1.lower()
print(str2)
Output:
hello from educba.
23. upper(): Meant to convert the entire string to upper case.
Example:
str1 = “Hello from EduCBA”
str2 = str1.upper()
print(str2)
Output:
HELLO FROM EDUCBA
24. replace(): Meant to replace a substring with another.
Example:
str1 = “Hello from EduCBA!”
str2 = str1.replace(“ from”,” there”)
print(str2)
Output:
Hello there EduCBA!
Conclusion
These are a few of the important string functions that are commonly used. Being through these methods makes it easier and faster for one to develop code when working with strings.
Recommended Articles
This has been a guide to Python String Functions. Here we discussed the basic concept with important Python String Functions in detail. You can also go through our other suggested articles to learn more –