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:
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 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.
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.
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.
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.
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 Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
- How to create perfect HTML tables?
- HTML color codes
- CSS background images
- Best Programming Books You Must Read in 2021
- Python for loop
- Creating a table using HTML and CSS
- Best way of using Java Arrays and ArrayLists
- Best way of using Python Sets
- Best ways of using a Python Dictionary
- Best way of using Python Classes
- Best way of using Python Range
- Best way of using Python if-else
- Best way of using Python RegEx
- Best way of using Python Lists
- Best way of using Python Enumerate
- Best way of using Python Functions
- Best way of using Python Split
- Best way of using Python Try-Except
- Best way of using Python Tuple
- Best way of using Python Arrays
- Best way of using Python Sort
- Best way of using Python DateTime
- How to download Python?
- Best way of using Python FileWrite
- Best way of using Python Lambda
- Best way of using Python ListAppend
- Best way of using Python ListComprehension
- Best way of using Python Map
- Best way of using Python Operators
- Best way of using Python Pandas
- Best way of using Python Requests
- Best way of using Python Strings
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.