String Slicing in Python

String Slicing in Python

String Slicing and Negative Index

·

4 min read

Learn Negative Index and String Slicing, I tried my best to make this comfortable for everyone but there might be mistakes so let me know that i'll prolly correct that in a month:(

Negative Index

As you know String's First Character's Index is 0, Second = 1, Third = 3 so on.. When we reverse this system it starts with Negative 1 which is called negative index

str1 = "Hello World"
print(str1[-11])
print(str1[0])

In this snippet Character H's index is 0 and -11 too.
Terminal:

H
H

If you didn't understood you can google :( and this png might help i guess

negative index.png let's do an example based on this string :)

str1 = "Python"
print(str1[0])
print(str1[-6])

Terminal:

P
P

Last one:)

str1 = "Python"
print(str1[-6])
print(str1[-5])
print(str1[-4])
print(str1[-3])
print(str1[-2])
print(str1[-1])

Terminal:

P
y
t
h
o
n

Let's Move to Slicing :)))))

Slicing

Slicing Strings means printing some part of String and removing rest, Slicing Makes Changes in final output it does not change whole string. First of all let's see how many characters are in the string we are gonna use

str1 = "Henry could not decide if he was an auto mechanic or a Chef."
print(len(str1))

Terminal:

60

They are 60
How Slicing is Defined: print(str1[StartingPoint:EndingPoint])
Let's Slice the String, If we want to print out only "Henry" we've to find Starting Index and Ending Index of that word, which is 0 and 4 respectively, But It Ignores the final Index so add one more index to print our word

str1 = "Henry could not decide if he was an auto mechanic or a Chef."
print(str1[0:5])

Terminal:

Henry

Null Slicing

Slicing Nothing = Null Slicing

str1 = "Henry could not decide if he was an auto mechanic or a Chef."
print(str1[:])

Terminal:

Henry could not decide if he was an auto mechanic or a Chef.

Undefined Index

If starting index is void then starting index will be 0(it is default)

str1 = "Henry could not decide if he was an auto mechanic or a Chef."
print(str1[:15])

Terminal:

Henry could not

If ending index is not defined it will be {characters in string or len} by default. len(str1)

str1 = "Henry could not decide if he was an auto mechanic or a Chef."
print(str1[0:])

Terminal:

Henry could not decide if he was an auto mechanic or a Chef.

For Negative Index

str1 = "Henry could not decide if he was an auto mechanic or a Chef."
print(str1[:-8])

Terminal:

Henry could not decide if he was an auto mechanic or

We've to count 1 index lesser than desired index (desired index - 1), like if we print -8 it will print till -9 in negative strings soo,

Skipping

Skipping Characters is similar to Slicing, print(str1[start : end : interval]) Let's try basic:

str1 = "Henry could not decide if he was an auto mechanic or a Chef."
print(str1[::])

Terminal:

Henry could not decide if he was an auto mechanic or a Chef.

Everything was default here and Interval is Set to 1 by default, let's try custom now.

str1 = "Henry could not decide if he was an auto mechanic or a Chef."
print(str1[::2])

Terminal:

Hnycudntdcd fh a nat ehnco  hf

it is Skipping every second character after :)))
Another Example:

str1 = "Henry could not decide if he was an auto mechanic or a Chef."
print(str1[0:15:2])

Terminal:

Hnycudnt

Reversing String

If we Set Interval to -1 it Completely Reverses String

str1 = "Henry could not decide if he was an auto mechanic or a Chef."
print(str1[::-1])

Terminal:

.fehC a ro cinahcem otua na saw eh fi ediced ton dluoc yrneH

What if we set to -2

str1 = "Henry could not decide if he was an auto mechanic or a Chef."
print(str1[::-2])

Terminal:

.eCar iacmou aswe ieie o lo re

String is Reversed and it is skipping every two characters

Might Help ;-;

It was too complicated for you i guess ;-'; work on slicing and negative strings, you'll get used to it :) ;-;

Thank You!