Python Strings are the sequence of alphabet characters. As you know in English, there are 26 letters. A Python string is created with these letters and other special characters with the help of quotation marks or double quotation marks. In Python Programming Course, we will give a lot of examples of these strings.
Normally computers work with binary numbers. They do not know strings. You see these strings but computer sees 1s and 0s. This conversion is called encoding. The reverse of it is decoding. ASCII and Unicode are two of the encoding mechanisms. Python uses Unicode characters for encoding. Because Unicode includes all the characters of all the languages.
To learn more about Python Strings, also visit the below lessons:
Table of Contents
So, how is a a string in Python? Below you can find an example for a Python String.
‘Bonjour’
Or
“Hola”
To print these strings, we can use print function.
The output will be hello word in French and in Spanish like below:
We can also assign a string to a variable. Below, we will assign Bonjour string to variable x.
We can also create a string that includes many lines as a paragraph. To do this we will use three double quotes (“””) or three single quotes (”’). We will write the string between three double quotes.
The output of this Python String example will be:
Again, the output will be the same.
To access a string element, we use square brackets. The characters in a string starts with index 0 and increase as index 1, index 2 etc. So, to access any letter of a string, we will write the previous number. For example, to access character 3, we will enter 2 inside the square brackets.
The output of this python programming code will be ‘l’ like below
Or to access the last item in a string, we can use -1 value inside the square brackets.
There are different Python String Methods used for various aims. For example, we can use these methods to find the length of a string. Or we can use it convert the string to upper case. We can count any string in a string, we can divide strings etc. To understand methods of python, let’s give examples.
We can find the length of a string in python. To do this, we will use string len method. This method counts the characters of the string including special characters and spaces. It counts anything between the quotation marks or double quotation marks.
The length of this string is
Now, let’s write a longer string with special characters and space.
The length of this string is
Converting a string to upper case, lower case and capitalize is done with different methods in python programming. To do this let’s give special examples.
Firstly, let’s capitalize all the letters in a string. Below, we will convert all the letters to upper cases.
The output of this code will be:
Now, let’s write only the first letter of a word with upper cases. To do this we will use title method
Lastly, let’s convert upper cases to lower cases again. Here, we will sue lower method of python strings.
In python programming, we can count a string in a string with count method. In other words, we can count a word in a string sentences series. Below, we will do an example to explain it better.
Here, we are counting routers keyword. So, the output of this code will be 2. Because there are two routers in this string.
Or, let’s count a word in a paragraph.
The output will be 4, because, there are four you word in this paragraph.
Another important point in Python Strings is string format. With this method, we can insert a string inside the specified place in the string. To do this, we use curly brackets {}, inside the string. And the place that we insert curly brackets, it puts the keyword that we use inside the parentheses.
Bellow, we will go to Lord of the Rings and add a word from this perfect movie.
Here, the code will insert Elf keyword, where ever it sees curly brackets {}. So, the output of this code will be like below:
In this lesson, we have learned what is a Python String. We have explained different python methods with different python string examples. To learn more about Python lessons, you can check the other detailed Python string lessons below:
Leave a Reply