A string is a sequence of one or more characters (letters, numbers, symbols) that can be either a constant or a variable. Made up of Unicode, strings are immutable sequences, meaning they are unchanging.
Because text is such a common form of data that we use in everyday life, the string data type is a very important building block of programming.
This Python tutorial will go over how to create and print strings, how to concatenate and replicate strings, and how to store strings in variables.
You should have Python 3 installed and a programming environment set up on your computer or server. If you don’t have a programming environment set up, you can refer to the installation and setup guides for a local programming environment or for a programming environment on your server appropriate for your operating system (Ubuntu, CentOS, Debian, etc.)
Strings exist within either single quotes '
or double quotes "
in Python, so to create a string, enclose a sequence of characters in one or the other:
Info: To follow along with the example code in this tutorial, open a Python interactive shell on your local system by running the python3
command. Then you can copy, paste, or edit the examples by adding them after the >>>
prompt.
'This is a string in single quotes.'
"This is a string in double quotes."
You can choose to use either single quotes or double quotes, but whichever you decide on you should be consistent within a program.
We can print out strings by calling the print()
function:
print("Let's print out this string.")
OutputLet's print out this string.
With an understanding of how strings are formatted in Python, let’s take a look at how we can work with and manipulate strings in programs.
Concatenation means joining strings together end-to-end to create a new string. To concatenate strings, we use the +
operator. Keep in mind that when we work with numbers, +
will be an operator for addition, but when used with strings it is a joining operator.
Let’s combine the strings "Sammy"
and "Shark"
together with concatenation through a print()
statement:
print("Sammy" + "Shark")
OutputSammyShark
If we would like a whitespace between the two strings, we can include the whitespace within a string, like after the word “Sammy”:
print("Sammy " + "Shark")
OutputSammy Shark
Be sure not to use the +
operator between two different data types. We can’t concatenate strings and integers together, for instance. So, if we try to write:
print("Sammy" + 27)
We will receive the following error:
OutputTypeError: Can't convert 'int' object to str implicitly
If we wanted to create the string "Sammy27"
, we could do so by putting the number 27
in quotes ("27"
) so that it is no longer an integer but is instead a string. Converting numbers to strings for concatenation can be useful when dealing with zip codes or phone numbers, for example, as we don’t want to perform addition between a country code and an area code, but we do want them to stay together.
When we combine two or more strings through concatenation we are creating a new string that we can use throughout our program.
##String Replication
There may be times when you need to use Python to automate tasks, and one way you may do this is through repeating a string several times. You can do so with the *
operator. Like the +
operator, the *
operator has a different use when used with numbers, where it is the operator for multiplication. When used with one string and one integer, *
is the string replication operator, repeating a single string however many times you would like through the integer you provide.
Let’s print out “Sammy” 9 times without typing out “Sammy” 9 times with the *
operator:
print("Sammy" * 9)
OutputSammySammySammySammySammySammySammySammySammy
With string replication, we can repeat the single string value the amount of times equivalent to the integer value.
Variables are symbols that you can use to store data in a program. You can think of them as an empty box that you fill with some data or value. Strings are data, so we can use them to fill up a variable. Declaring strings as variables can make it easier for us to work with strings throughout our Python programs.
To store a string inside a variable, we need to assign a variable to a string. In this case let’s declare my_str
as our variable:
my_str = "Sammy likes declaring strings."
Now that we have the variable my_str
set to that particular string, we can print the variable like so:
print(my_str)
And we will receive the following output:
OutputSammy likes declaring strings.
By using variables to stand in for strings, we do not have to retype a string each time we want to use it, making it better for us to work with and manipulate strings within our programs.
This tutorial went over the basics of working with the string data type in the Python 3 programming language. Creating and printing strings, concatenating and replicating strings, and storing strings in variables will provide you with the fundamentals to use strings in your Python 3 programs.
Continue learning more about strings by taking a look at the following tutorials:
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
A string is a sequence of one or more characters (letters, numbers, symbols) that can be either a constant or a variable. Made up of Unicode, strings are immutable sequences, meaning they are unchanging.
Because text is such a common form of data that we use in everyday life, the string data type is a very important building block of programming.
This tutorial series will go over several of the major ways to work with and manipulate strings in Python 3.
Python is a flexible and versatile programming language that can be leveraged for many use cases, with strengths in scripting, automation, data analysis, machine learning, and back-end development. It is a great tool for both new learners and experienced developers alike.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.