Python strings are characters enclosed in quotes of any type – single quotation marks, double quotation marks, triple quotation marks.
is the best free online academy on YouTube ’’’
Python strings is an immutable data type, i.e. not allowed to change in place. S[0] =‘R’ not allowed
Strings are sequence of characters, where each character has a unique index number.
string = “mycstutorial.in”
>>> string[0]
‘m’
>>> string[-1]
‘n’
Two types of Indexing is supported in python.
The indexes of strings begin from 0 to length-1, in forward direction and -1, -2, -3, …. , -length in backward direction.
Traversing a String or Text means accessing all characters of string, character by character, through its index position.
#Traversal of a string using loop
name = "mycstutorial.in"
for char in name:
print (char, end=' ')
Output
m y c s t u t o r i a l . i n
#Traversal of a string using index - forward indexing
name = "mycstutorial.in"
for index in range(len(name)):
print (name[index], end=' ')
Output
m y c s t u t o r i a l . i n
#Traversal of a string using index - backward indexing
name = "mycstutorial.in"
for index in range(-1, -len(name)-1, -1):
print (name[index], end=' ')
Output
n i . l a i r o t u t s c y m
Slice means ‘a part of’. In Python, string slice refers to a part of string containing some contiguous characters from the string.
Syntax: string[start : end : step]
String Built-in functions
84 videos|19 docs|5 tests
|
|
Explore Courses for Grade 11 exam
|