December 16
Edit

How To Slice Strings in Python 3 by Index

S a m m y S h a r k !
0 1 2 3 4 5 6 7 8 9 10 11

Accessing Characters by Positive Index Number

print(ss[4])  ==>  y

Accessing Characters by Negative Index Number

print(ss[-3])  ==>  r

Slicing Strings

print(ss[6:11])  =>  Shark
print(ss[:5])  ==>  Sammy
print(ss[7:])  ==>  hark!
print(ss[-4:-1])  ==>  ark

Specifying Stride while Slicing Strings

print(ss[6:11])  ==>  Shark
print(ss[6:11:1])  ==> Shark
print(ss[0:12:2])  ==>  SmySak
print(ss[0:12:4])  ==>  Sya
print(ss[::4])  ==>  Sya
print(ss[::-1])  ==>  !krahS ymmaS
print(ss[::-2])  ==>  !rh ma

Counting Methods

print(len(ss))  ==>  12
print(len("Let's print the length of this string."))  ==>  38
print(ss.count("a"))  ==> 2
print(ss.count("s"))  ==>  0

likes = "Sammy likes to swim in the ocean, likes to spin up servers, and likes to smile."

print(likes.count("likes"))  ==>  3
print(likes.find("likes"))  ==>  6     #the first “likes” character sequence occurs
print(likes.find("likes", 9))  ==>  34    #the first “likes” character sequence occurs after position 9
print(likes.find("likes", 40, -6))  ==>  64   #

We can check to see where the first “m” occurs in the string ss:

print(ss.find("m"))  ==>  2

Send us a message. We will reply as soon as we can.