Strings are ordered sequence of characters , using the syntax of single quotes or double quotes . 'hello' , "world" , " didn't " are all examples of a string. Since strings are ordered sequences , operations like indexing and slicing can be performed on them.
Indexing is used to grab a single character of a string. Indexing uses [ ] notation after the string( or string assigned variable). Within the [ ] , the index of specified character is mentioned. In Python , indexing starts from zero and goes till (length of string - 1) . This will become more clear through an example. Consider the string "Hero" , the indexing position for 'H' is 0 , for 'e' it is 1, for 'r' it is 2 and for 'o' it is 3.
Let us write code for this example and print each character of the string "hero" using indexing.
mystring = "hero"
print(mystring[0])
print(mystring[1])
print(mystring[2])
print(mystring[3])
h
e
r
o
print("hero"[0])
print("hero"[1])
print("hero"[2])
print("hero"[3])
Say you want to grab the alphabet 'o' from the word "hero" , you can simply write "hero"[3] to get the output or you can write "hero"[-1] to get the same output. Please note that backward indexing starts from -1 and not from 0. Similarly you can write "hero"[-2] to get 'r' from "hero".
mystr = "hero"
print(mystr[-1])
print(mystr[-2])
print(mystr[-3])
print(mystr[-4])
o
r
e
h
Slicing(to cut a part of the string) allows you to grab a subsection made up of multiple characters from the string. It has the following syntax : string[Start:Stop:Step]
- Start is the numerical index for slice start.
- Stop is index you will go up to (but not including it).
- Step represents the size of jump you want to take when going from the starting index to the stopping index.
Let us write a code to clearly understand these points. In the following code we are given a string "helloworld" and we are performing following operations:
- line 1 : string is assigned to the variable named mystr
- line 2 : printing out hello on the screen
- line 3 : printing out world on the screen
- line 4 and line 5 : both the lines represent the same code.Print the string by a jump of 2
- line 6 : printing the reversed string ( "dlrowolleh")
mystr = "helloworld"
print(mystr[0:5])
print(mystr[5:])
print(mystr[0::2])
print(mystr[::2])
print(mystr[::-1])
hello
world
hlool
hlool
dlrowolleh
To grab "hello" from mystr , we need to specify the starting index as 0 and stoping index as 6 ( not 5 since the stoping index itself is never included in the slicing range so we always add 1 to index of ending character) also here we don't need the step value. To grab "world" we simply need to specify the starting index(which in this case is 5) and not bother about the stoping index(since we need to grab the string till the end from w). Note that mystr[5:10] would also give the output as "world". To print the string by leaving out the next character , simplify specify the step value as 2.Note that if starting indexing is not mentioned than simply assume 0. To print the string in reversed order simply write mystr[::-1].