Slicing


Slicing


str = 'some text' 

print(str[0:4])

The first character position is 0 and the length is 4



_var = "Welcome" 
print(_var[3:5]) # 'co'

_var = "Welcome" 
print(_var[3:]) # 'come'

_var = "Welcome" 
print(_var[3]) # 'c'

_var = "Welcome" 
print(_var[0]) # 'w'


_var = "   Welcome   " 
print(_var.strip()) # 'Welcome'



Strings slicing

person_name = "Grace Hopper" 
print(person_name[0]) #= "G" first character
print(person_name[1]) #= "r" second character
print(person_name[2]) #= "a" third character


Strings negative indices

word = "hamster" 
print(word[-1]) #= r
print(word[1:-1]) #= amste
print(word[-3:]) #= ter
print(word[:3]) #= ham




© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext