In this lesson we want to learn about Python String Methods.
What is Python String ?
In Python string is a sequence of characters enclosed within single, double or triple quotes. Strings can contain alphanumeric characters, symbols and punctuation marks. They are used to represent text or a combination of characters. In Python, strings are immutable objects, meaning their value cannot be changed once they have been created. The built-in str
class in Python provides many methods to manipulate and format strings.
Learn More on Python
- How to Convert Integer to Roman in Python
- How to Sort Words in Python
- Python Best Libraries for Web Development
- Top 10 Python REST API Frameworks
- Python Random Module
- Python Dictionary Methods
Python String Methods
Python has several built in methods for working with strings. these are some of the most commonly used string methods:
str.capitalize()
: Returns a copy of the string with the first character capitalized.str.center(width, fillchar)
: Returns a string of specified width, padded with the specifiedfillchar
(or spaces by default) so that the original string is centered.str.count(sub, start, end)
: Returns the number of non-overlapping occurrences ofsub
in the string, between the indicesstart
andend
.str.endswith(suffix, start, end)
: ReturnsTrue
if the string ends with the specifiedsuffix
, between the indicesstart
andend
.str.find(sub, start, end)
: Returns the lowest index in the string wheresub
is found, between the indicesstart
andend
. Returns -1 ifsub
is not found.str.format(*args, **kwargs)
: Returns a string with placeholders replaced by the specified arguments.str.isalnum()
: ReturnsTrue
if all characters in the string are alphanumeric (letters or digits).str.isalpha()
: ReturnsTrue
if all characters in the string are alphabetical.str.isdigit()
: ReturnsTrue
if all characters in the string are digits.str.islower()
: ReturnsTrue
if all characters in the string are lowercase.str.join(iterable)
: Concatenates all elements in theiterable
into a single string, separated by the original string.str.replace(old, new, count)
: Returns a copy of the string with all occurrences ofold
replaced withnew
. The optionalcount
argument specifies the maximum number of replacements to make.
This is an example of using some of the string methods:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# Define a string message = "Hello, World!" # Use the capitalize() method to capitalize the first character capitalized_message = message.capitalize() print(capitalized_message) # Use the count() method to count the number of 'l' characters count = message.count('l') print(count) # Use the endswith() method to check if the string ends with '!' ends_with_exclamation = message.endswith('!') print(ends_with_exclamation) # Use the format() method to replace placeholders with values formatted_message = "{} is a great place to learn {}!".format("OpenAI", "AI") print(formatted_message) # Use the join() method to concatenate a list of strings into a single string words = ["Welcome", "to", "the", "world", "of", "AI"] joined_words = " ".join(words) print(joined_words) |
In this example, we define a string message
and use several string methods to perform operations on the string. We use the capitalize()
method to capitalize the first character of the string, the count()
method to count the number of 'l'
characters in the string, the endswith()
Run the complete code and this will be the result