Definition of Python 3 String Methods
Python 3 string methods are one of Python’s most common data types. Simply wrapping characters in quotations allows us to construct them. Assigning a value to a variable is the same as creating strings. A string in Python is a collection of Unicode characters surrounded by quotation marks. Every string method altered characteristics rather than modifying the original string. There are multiple string methods are available in python.
Introduction to python 3 string methods
- Python supports a character type, these are handled as one-length strings and thus considered substrings. To get a substring, use square brackets for slicing and the index or indices.
- A string is a collection of characters surrounded by quotation marks to concatenate two strings, for instance, use the join method. Python 3 string method is very important and useful in python, different methods have a different use.
Python 3 string methods list
- Below are string methods available in python are as follows. String methods are very important in python.
1. Capitalize
- Returns a string copy with the first letter capitalized and the remainder lowercased.
- If we wish all words’ first letters capitalized, use title. The below example shows capitalize string methods in python.
Code:
py = "python 3 string methods"
print (py.capitalize ())
py = "python 3 string methods"
print (py.title())
Output:
2. Casefold
- Returns a string that has been case-folded. For caseless matching, casefolded strings can be utilized.
- The below example shows casefold string methods in python.
Code:
py = "Python String Methods"
py1 = py.casefold()
print(py1)
Output:
3. Center
- Returns the centered string in a length width string. The provided fill char can be used to add padding. If width equals or is less than len.
- The below example shows center string methods in python.
Code:
py1 = "python"
py2 = py1.center(10, "+")
print (py2)
Output:
4. Count
- Non-overlapping substring occurrences in the range [start, end]. Start and end are optional arguments that are interpreted as slice notation.
- Python will not count characters twice due to non-overlapping occurrences. The below example shows count string methods in python are as follows.
Code:
py = "Python String Methods"
print(py.count("S"))
print(py.count("o"))
print(py.count("t"))
Output:
5. Encode
- As a bytes object, returns a string that has been encoded. utf-8 is the standard encoding. To set a custom error handling scheme, errors can be provided.
- The below example shows that encode string methods in python are as follows.
Code:
from base64 import b64encode
py = "Python"
print(py)
py = b64encode(py.encode())
print(py)
Output:
6. Endswith
- True is the result. Otherwise, False is returned if the text does not conclude with the required suffix. The test starts at that location if the start parameter is specified. The test ends comparing at that location when we select optional end.
- The below example shows endswith string methods in python are as follows.
Code:
py = "Python"
print(py.endswith("on"))
print(py.endswith("n"))
print(py.endswith("no", 1, 3))
Output:
7. Expandtabs
- The specified tab size returns a copy of the string with all tab characters replaced by one or more spaces. Every tabsize character has a position.
- The below example shows expandtabs string methods in python are as follows.
Code:
py = "py\tth\ton"
print(py)
print(py.expandtabs())
print(py.expandtabs(tabsize=14))
print(py.expandtabs(tabsize=4))
Output:
8. Find
- Returns the string’s lowest index where the substring sub is located within the slice.
- The below example shows find string methods in python are as follows.
Code:
py = "Python"
print(py.find("P"))
print(py.find("o"))
print(py.find("n"))
Output:
9. Format
- This method applies string formatting. This method can be invoked on a string that contains either literal text or brace-delimited replacement fields.
Code:
print("{} Python {}".format("String", "Methods"))
Output:
10. Index
- If we have not found any substring this string in python will raise an error are as follows.
Code:
py = "Python"
print(py.index("P"))
print(py.index("h"))
11. Isalnum
- If the string has all of the characters are alphanumeric, it returns True. Otherwise, returns False.
Code:
py = "Python"
print(py.isalnum())
Output:
12. Isalpha
- If the string has all of the characters are alphabetic, it returns True. Otherwise, returns False.
Code:
py = "Python"
print(py.isalpha())
Output:
13. Isdecimal
- Returns true if the string contains all of the characters are decimal characters. Otherwise, returns False.
Code:
py = "Python"
py1 = "752"
print(py.isdecimal())
print(py1.isdecimal())
Output:
14. Isdigit
- Returns true if the string contains all of the digits. Otherwise, returns False.
Code:
py = "Python"
py1 = "752"
print(py.isdigit())
print(py1.isdigit())
Output:
15. Islower
- Returns true if the string contains all lowercase letters. Otherwise, returns False.
Code:
py = "Python"
py1 = "python"
print(py.islower())
print(py1.islower ())
Output:
16. Isupper
- Returns true if the string contains all uppercase letters. Otherwise, returns False.
Code:
py = "PYTHON"
py1 = "python"
print(py.isupper())
print(py1.isupper())
Output:
Examples
Below are the different examples mentioned:
Example #1
The below example shows join python string methods are as follows.
Code:
py = "*"
print(py.join("975"))
py1 = "-"
print(py1.join("python"))
Output:
Example #2
The below example shows the isnumeric string method in python which is as follows.
Code:
py = "742"
print(py.isnumeric())
py1 = "Python3"
print(py1.isnumeric())
Output:
Example #3
The below example shows isprintable methods in python are as follows.
Code:
py = "\t"
print(py.isprintable())
py1 = "Python3"
print(py1.isprintable())
Output:
Example #4
The below example shows isspace string method in python is as follows.
Code:
py = ""
print(py.isspace())
py1 = " "
print(py1.isspace())
Output:
Example #5
The below example shows istitle string method in python is as follows.
Code:
py = " Python"
print(py.istitle())
py1 = "python"
print(py1.istitle())
Output:
Conclusion
There are multiple string methods are available in python. To concatenate two strings, for instance, use the join method. A string in Python is a collection of Unicode characters surrounded in quotation marks. Python 3 string methods are one of Python’s most common data types.
Recommended Articles
This is a guide to Python 3 string methods. Here we discuss the definition, Python 3 string methods list, examples with code implementation. You may also look at the following articles to learn more-