Python String Functions: 12 Interactive Examples

Download Now: Free Introduction to Python
Danielle Richardson Ellis
Danielle Richardson Ellis

Published:

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.

Developer using list of python string functions to assist with programming.

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.Download Now: An Introduction to Python [Free Guide]

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

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

Try with the code module

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!

Try with the code module

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']

Try with the code module

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

Try with the code module

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

Try with the code module

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

Try with the code module

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

Try with the code module

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

Try with the code module

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

Try with the code module

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

Try with the code module

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”

Try with the code module

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"

Try with the code module

python

Topics: What Is Python?

Related Articles

We're committed to your privacy. HubSpot uses the information you provide to us to contact you about our relevant content, products, and services. You may unsubscribe from these communications at any time. For more information, check out our Privacy Policy.

A guide for marketers, developers, and data analysts.

CMS Hub is flexible for marketers, powerful for developers, and gives customers a personalized, secure experience

START FREE OR GET A DEMO