Python String Slice


Python String Values

In this lesson, we will focus on the values of the python strings. In other words we will see how to access substrings in the Python Strings. Beside, we will see python string slice process. We will learn how to slice python strings and pring these sliced values.


You can also learn Python String Concatenation


How to Access Values in Python Strings?


In python strings, we can access a specific value in the string. In other words, we can access substrings in the python strings with the help of square brackets. So, how can we access values in Python strings with the help of these square brackets?


Python strings is an arrays. They include characters and each of this character has a position in this array. The first character of this array is in index 0 and the second one is in index 2 and so on. So, to access a value in Python string, we will give the previous index of this value.


For example, to access the third character of the python string “Hello World!”, we will use index 2. Here,

1.character – index 0 is H,

2.character – index 1 is e,

3.character – index 2 is l

and so on.


Here, we can use below python code to access the third character of the string:


x = "Hello World!"


And this code, will print “l” character.







You can also learn the other methods of Python Strings


We can also use negative numbers to access any python string value. If we use a negative number as index, this means that,we will start to count from the last string value.


You can also watch this lesson on Youtube!


For example, if we use -1 as index, this means that, we are looking fort he last character.


x = "Hello World!"


The above python code will prinf the last character of the string. This will be “!”.




Again, if we use -2 as index, it will print “d”, if we use -3 as index, it will print “l” and so on.


These index vlues will also used for python string slice process. Let’s also learn how to slice python strings.



Python String Slice


Above, we have talked about the index values used with strings to access the substrings in the strings. Or in other words, to access the characters of the python strings. Here, we will focsu on python string slice. We will learn how to slice strings in python.


For python string slice, we will use colon with the beginning index and the last index of the python string.


For example, if we use the index 3 as beginning index and index 5 as last index like below;


x = "Hello World!"


this python code will pring “lo” sliced string.






In the above example, we use both beginning and the last index. We can do python string slice without one of these indexes. If we use only the last character, we will see the characters from the beginining of the python string to this index.


For example, if we use 8 index as tha last index like x[:8], the sliced string will be Hello Wor.


x = "Hello World!"


Here, do not forget to start form the index 0 and count also the space in the string.


Hello Wor




If we use only the beginning index, then the sliced string will start from this index and go through the end of the string.


x = "Hello World!"


lo, World!




As you can see, the sliced python string starts from the third value of the string and will be printed through end of the string as above.


About python string slice, let’s talk about the negative indexes laslty. We can also use negative index values to slice python strings. This time the values of the beginning and the last index will Show the beginning and the last character of the string.


For example,


x = "Hello World!"



The above code will give the below output:


o Worl




You can practice on this method with the other lessons of this Python Programming Course.



Back to: Python Programming Course > Python Strings

Leave a Reply

Your email address will not be published. Required fields are marked *

Python Programming Course