How to use Python strings – with example

  • Reading time:16 mins read

What is a Python string?

A string is a sequence of characters that are enclosed by a single or double quote.



In Python, a string is created by creating a variable where we’re going to save our string which is basically just some characters put together into words or other things.


Learn Python Strings online


Creating Python strings

Creating a variable simply involves initializing a variable that is going to store our string. For instance, if we create the variable ‘animal’ this is going to be the variable that we’re storing our string in. Strings are commonly created using single quotes:


Double quote marks

However, we can also use double quote marks but we can’t mix double quotes with single quotes in a single string. This will give you an error:


Triple quote marks

Triple quote marks can also be used to create strings in Python. And the good thing about the triple quote marks is that it allows us to write strings that span through multiple lines.

This is particularly useful for commenting out large blocks of code or maybe writing something at the top of your script that you need to reference to print this out. We can simply just type print and then the name of our variable.


Indexing Python strings

Strings can also be indexed just in the same way we would do with a list or a dictionary in Python. Normally when indexing we use the square brackets and then we would use the index number to reference each letter. Now, python is a zero index language. So the first ‘e’ in this case will be zero. 



Negative Indexing

We can also access elements at the end of a string using negative integers. However, unlike when accessing elements from the start of a string the last element of a string is represented by index -1.


Python Strings are useful tools while coding

Immutable strings

It’s worth noting that a string is immutable. That means that once it’s been assigned, we can’t actually change it. We can simply just need to reassign the variable to this to a new string to get rid of it.


Python String Concatenation

Strings can also be added together. So if we add another string called ‘age’ we can combine the two strings using the plus sign.


Adding spaces to our string

Now there’s no gap between the two strings that we have just concatenated. If we wanted to have a space in our string, we would simply just add in space and we can concatenate these together because they’re all strings.


Python String Repetition

Now we have a space between the two strings. We can also repeat a string multiple times without having to type in every time we need the string repeated.


Learn Python strings online


In the example above we can repeat the variable combined five times by adding an asterisk followed by 5 which will then have the word elephant printed out five times.


Concatenate strings with integers

Strings cannot be concatenated together with integers. This is a common problem that arises when dealing with different bits of data from different places.



So to avoid this error instead of changing the information we can just wrap the integer in the word str(). This is going to convert the integer value for age into a string.



If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!





Not sure if programming is for you? With CodeBerry you’ll like it.