In coding, strings are sequences of characters located on the same line of code. They're immutable, meaning their value can't be changed without creating a new value altogether.
Python comes equipped with a series of string functions that you can use to perform all types of tasks on your website. You can edit text, change the format of different characters, and customize the look of your copy based on different inputs in your code.
We've curated the following list of Python string functions as a reference sheet that you can use as you code. Use this list as well as the interactive code module to familiarize yourself with string functions and become a more proficient Python programmer.
Python String Functions
string.capitalize() | strip() | lower() |
input() | upper() | find() |
split() | join() | format() |
str() | title() | reversed() |
Use this table to navigate to specific Python functions. Try out the functions below by copying the code example into this interactive code module. Execute the code's output by clicking the play (horizontal triangle) button.
1. Capitalize Function: string.capitalize()
The capitalize() function returns the first character of a string as an uppercase letter and the rest of the characters as lowercase letters.
Capitalize Function Example:
mystatement = "welcome to hubspot"
myvalue = mystatement.capitalize()
print(myvalue)
Output:
Welcome to hubspot
2. Input Function: input()
The input() function receives user input and returns the input as a string.
Input Function Example:
print("What is your favorite color?")
x = input()
print(x + " is my favorite color!")
Output:
What is your favorite color?
Blue
Blue is my favorite color, too!
3. Split Function: split()
The split() function breaks bigger strings into smaller strings by splitting a string into a list.
Split Function Example:
statement = "python is fun and easy"
myvalue = statement.split()
print(myvalue)
Output:
['python', 'is', 'fun', 'and', 'easy']
4. String Function: str()
The string or str() function turns a specified object into a string.
String Function Example:
myvalue = str(5)
print(myvalue)
Output:
5
5. Strip Function: strip()
The strip() function or method removes all of the leading and trailing characters from a string.
Strip Function Example:
mystatement = ' Removes all the unnecessary spaces '
print(mystatement.strip())
Output:
Removes all the unnecessary spaces
6. Uppercase Function: upper()
The uppercase or upper() function converts all of the letters in a string to uppercase.
Uppercase Function Example:
mystatement = 'welcome to hubspot'
print(mystatement.upper())
Output:
WELCOME TO HUBSPOT
7. Join Function: join()
The join() function returns a string by joining all items in an iterable together.
Join Function Example:
dimensions = ("4", "6", "8")
x = "x".join(dimensions)
print(x)
Output:
4x6x8
8. Title Function: title()
The title() function capitalizes the first character of every word in a string.
Title Function Example:
y = "the hubSpot website blog"
x = y.title()
print(x)
Output:
The HubSpot Website Blog
9. Lowercase Function: lower()
The lower() or lowercase function returns a string where all characters are lowercase.
Lowercase Function Example:
x = 'WELCOME TO HUBSPOT'
print(x.lower())
Output:
welcome to hubspot
10. Find Function: find()
The find() function searches for the given string and returns its index in the Python string.
Find Function Example:
my_str = "Python is a powerful language"
index_result = my_str.find("language")
print(index_result)
Output:
21
11. Format Function: format()
The format() function is used to format Python strings. It allows you to specify the data type, width, and precision of the output string.
Format Function Example:
name = "John"
age = 20
my_str= "{} is {} years old".format(name, age)
print(my_str)
Output:
“John is 20 years old”
12. Reversed String Function: reversed()
The reversed() function is used to reverse a string. It takes the string as an argument and returns a reversed version of the same string.
Reversed String Function Example:
my_string = "Hello World"
reversed_string = reversed(my_string)
print("".join(reversed_string))
Output:
"dlroW olleH"