python

Learn to Use Python's Built-In String Functions

There are several built-in string functions in Python that can be used to manipulate and work with strings. Here is a list of some of the most commonly used string functions:


len() - returns the length of a string.

my_string = "Hello World!"
length = len(my_string)
print(length)  

# Output: 12

str() - converts a value to a string.

number = 123
my_string = str(number)
print(my_string)  # Output: "123"

boolean = True
my_string = str(boolean)
print(my_string)  # Output: "True"

lower() - converts a string to lowercase.

my_string = "HELLO, WORLD!"
lowercase = my_string.lower()
print(lowercase)  # Output: "hello, world!"

upper() - converts a string to uppercase.

my_string = "hello, world!"
uppercase = my_string.upper()
print(uppercase)  # Output: "HELLO, WORLD!"

capitalize() - capitalizes the first letter of a string.

my_string = "hello, world!"
capitalized = my_string.capitalize()
print(capitalized)  # Output: "Hello, world!"

title() - capitalizes the first letter of each word in a string.

my_string = "hello, world!"
title_case = my_string.title()
print(title_case)  # Output: "Hello, World!"

strip() - removes leading and trailing whitespace from a string.

my_string = "   Hello, World!   "
stripped = my_string.strip()
print(stripped)  # Output: "Hello, World!"

lstrip() - removes leading whitespace from a string.

my_string = "   Hello, World!   "
stripped = my_string.lstrip()
print(stripped)  # Output: "Hello, World!   "

rstrip() - removes trailing whitespace from a string.

my_string = "   Hello, World!   "
stripped = my_string.rstrip()
print(stripped)  # Output: "   Hello, World!"

replace() - replaces a specified substring with another string.

my_string = "Hello, World!"
new_string = my_string.replace("Hello", "Goodbye")
print(new_string)  # Output: "Goodbye, World!"

find() - returns the index of a specified substring within a string.

my_string = "Hello, World!"
index = my_string.find("World")
print(index)  # Output: 7

index() - returns the index of a specified substring within a string.

my_string = "Hello, World!"
index = my_string.index("World")
print(index)  # Output: 7

count() - counts the number of occurrences of a specified substring within a string.

string.count() is a built-in Python string function that returns the number of occurrences of a specified substring within a string.

Here is an example of how to use string.count():

my_string = "Hello, World! Hello, World!"
count = my_string.count("Hello")
print(count)  # Output: 2

You can also specify a start and end index to limit the search to a specific part of the string:

my_string = "Hello, World! Hello, World!"
count = my_string.count("Hello", 0, 14)  # search only the first 14 characters
print(count)  # Output: 1

You can also use string.count() with regular expressions to search for patterns in a string:

my_string = "Hello, World! Hello, World!"
count = len(re.findall("Hello", my_string))  # use regular expression to find all occurrences of "Hello"
print(count)  # Output: 2

These functions can be used to perform various operations on strings, such as formatting, searching, and manipulating text. For example, you can use the strip() function to remove unnecessary whitespace from a string, or the replace() function to replace specific substrings with other strings. You can find more information about these and other string functions in the Python documentation.

Was this helpful?