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.
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.
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.
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.
Summary
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
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to use the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
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!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING
Not sure if programming is for you? With CodeBerry you’ll like it.
