Cyberithub

Python: Introduction to Strings and Slices with Examples

Advertisements

In this article, I will take you through the Introduction to Strings and Slices with Examples. Like many other programming languages, Python also has one of the most important data type called String. But as you might have noticed, unlike other programming languages python does not have char data type. A character in Python is considered as String. It is a very useful data type which developers uses very often in their code. Due to its large scale usage in complex applications, python provides many In built functions and shortcuts which can be used to manipulate strings. We will go through each of them in great detail here.

Python: Introduction to Strings and Slices with Examples

Python: Introduction to Strings and Slices with Examples

Also Read: Python: Variables, Data Types and Conditionals with Examples

1. Strings

Strings are a sequence of characters that can store letters, numbers, punctuation or a combination. The way we indicate a string is by surrounding it with quotes - either single or double quotes.

Using Single Quotes

>>> 'Hello World'
'Hello World'

Using Double Quotes

>>> "Hello World"
'Hello World'

In the above examples, characters inside single and double quotes are known as Strings. Just like numbers, we can store strings in a variable. This can be explained using below example.

>>> fname = 'John'
>>> lname = 'Wick'
>>> Name = fname + lname
>>> Name
'JohnWick'

In the above example, we are storing string John in the fname variable and Wick in lname variable. Then we are concatenating both the variables to get the complete name and storing it in Name variable. This can be referenced to access the string stored in Name variable which will give us JohnWick on the output. However, you might have noticed that there is no space between string John and Wick. So to put a space in between we have to use like below.

>>> fname = 'John'
>>> lname = 'Wick'
>>> Name = fname + ' ' + lname
>>> Name
'John Wick'

 

2. Using Quotes in Strings

Whenever we try to use quote inside the strings then it always throw Invalid Syntax error as you can see below.

>>> nissan = 'World's Class Car'
File "<stdin>", line 1
nissan = 'World's Class Car'
^
SyntaxError: invalid syntax

To fix above issue, it is always recommended to use double quotes around the string so that Python will look for opening and closing of double quotes.

>>> nissan = "World's Class Car"
>>> nissan
"World's Class Car"

 

3. String - Behind the Scenes

Behind the Scenes, String is nothing but a list of characters and each character in this list has a position or an index. This can be explained with the help of below example.

painter = 'Pablo Picasso'

In the above string, you can check the position of each and every character by referencing the painter variable like below.

>>> painter = "Pablo Picasso"
>>> painter[0]
'P'
>>> painter[1]
'a'
>>> painter[2]
'b'
>>> painter[3]
'l'
>>> painter[4]
'o'
>>> painter[5]
' '
>>> painter[6]
'P'
>>> painter[7]
'i'
>>> painter[8]
'c'
>>> painter[9]
'a'
>>> painter[10]
's'
>>> painter[11]
's'
>>> painter[12]
'o'

 

4. String Built In Function - len()

String has one very important built In Function called len(). Using this function, one can find out the length of the String. This can be explained using the previous painter example. If you check the length of the string stored in painter variable using len(painter) command from Python Interpreter, then it will show the length as 13.

>>> len(painter)
13

 

5. Using Slices to Access Parts of a String

Python provides excellent feature of slicing parts of a String. One can specify the start and end of the slice boundary to cut out parts of the String. This can be understood further by using below example.

>>> word = "Hello World !!"
>>> word[2:5]
'llo'

In the above example, we have a string called 'Hello World !!' stored in the variable word. Now if you want to slice the parts of this string from index 2 to 5 then you need to use word[2:5] as shown above. There is actually a formula that you can use to slice the characters from a string and it goes like below.

Slice formula:  variable [start: end+1]

Let's see some more examples:-

>>> word[2:]
'llo World !!'
>>> word[:5]
'Hello'

You can also use some shortcuts like above where you can only mention the start boundary or the end boundary and get the rest of the characters from the String. More about String Slicing.

Leave a Comment