All About Strings in Python


Python has a built-in string class named “str” with many handy features (there is an older module named “string” which you should not use). The string class is available by default in python, so you do not need an import statement to create string objects.

In this blog post, we’ll be discussing everything there is to know about strings in Python. This includes how to create them, what operations can be performed on them, and some of the caveats that you should be aware of. By the end of this post, you’ll be a string expert! 🙂

Creating Strings in Python

There are a few ways to create strings in Python. The most common way is to use quotation marks:

single_quoted_string = 'data science' double_quoted_string = "machine learning"
Code language: JavaScript (javascript)

You can also use triple quotes:

triple_quoted_string = """this is a long string that goes over multiple lines"""
Code language: Python (python)

If you need to use quotation marks as part of the string, you can escape them with a backslash:

escaped_quotes = "this string has \"escaped\" quotes"
Code language: JavaScript (javascript)

Operations on Strings in Python

Once you have a string, there are many things that you can do with it. For example, you can find its length using the len() function:

single_quoted_string = 'data science' len(single_quoted_string) # returns 12
Code language: PHP (php)

You can also concatenate two strings together using the + operator:

new_string = single_quoted_string + " " + double_quoted_string # returns "data science machine learning"
Code language: Python (python)

String Formatting

Another way to concatenate strings in Python is by using string formatting. With string formatting, you can place placeholders in your string, and then insert values into those placeholders at runtime.

Here’s an example of how to format a string in Python:

my_name = 'John Doe' my_age = 20 my_string = f'my name is {my_name} and I am {my_age} years old.'
Code language: Python (python)

String Slicing

You can return sections or slices of a string by using square brackets ([ ]).

my_string = "This is MY string!" print(my_string[0:4]) # From the start till before the 4th index print(my_string[1:7]) print(my_string[8:len(my_string)]) # From the 8th index till the end
Code language: Python (python)

Read more: How to Slice String in Python?

For example: If you need to check if a particular substring is present in a string, you can use the in keyword:

substring = "science" If substring in new_string: # returns True print("found it!") # this code will execute because "science" is in new_string else: print("nope") # this code will NOT execute because "science" is in new_string
Code language: Python (python)

Finally, if you want to get all of the characters in a string as a list, you can use the list() function:

list(new_string) # returns ['d', 'a', 't', 'a', ' ', 's', 'c', …etc]
Code language: CSS (css)

Caveats With Strings in Python

Although strings are one of the most commonly used data types in Python, they also have some limitations. For example, once you create a string, you cannot modify it (this is called immutable). This means that if you want to change part of a string, you need to create an entirely new string:

modified_string = new_string.replace("machine learning", "artificial intelligence")
Code language: Python (python)

This limitation can be annoying at times but it also provides some benefits. Because strings cannot be modified after they are created, we don’t have to worry about another part of our code accidentally modifying a string that we intended to stay constant. This makes our code more robust and less likely to contain bugs.

Conclusion

Strings are one of the most commonly used data types in Python and they are incredibly versatile. In this post, we covered everything from how to create strings to some of the operations that can be performed on them. We also discussed some of the caveats with strings (namely that they are immutable). Now that you’re armed with this knowledge, go forth and start working with strings like a pro!

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts