To read previous part of using Python Shell for working with strings: Part -1 click here
By default strings are indexed. Indexing starts with the first character as index 0. Note: in Python there is no separate data type as character, a character is simply a string of size one
In Python indices can also be negative numbers, negative number indices are used to start counting from the right of the string. Take a look at Example at 5
Note that since -0 is the same as 0, negative indices start from -1.
Example 5:
>>> m='Meetu'
>>> m[0] #first character of variable m
'M'
>>> m[4] #Last Character of variable m
'u'
>>> m[-1] #Last character of variable m using negative indices
'u'
>>> m[-5] #First character of variable m using negative indices
'M'
>>>
With indexing Python also supports slicing. This means extracting more than one character form the strings also called as substring.>>> m[0] #first character of variable m
'M'
>>> m[4] #Last Character of variable m
'u'
>>> m[-1] #Last character of variable m using negative indices
'u'
>>> m[-5] #First character of variable m using negative indices
'M'
>>>
Example 6:
>>> m= 'Meetu Choudhary' #Setting Variable m
>>> m[0:4] #Slicing [Extracting first 4 Characters]
'Meet'
>>> m[6:15] #Slicing [Extracting characters from middle of the string till end]
'Choudhary'
>>>#Above two statements using print() function
>>> print(m[0:4])
Meet
>>> print(m[6:15])
Choudhary
>>> m='Gaurav Nanda' #resetting variable m value
>>> print(m[8:11]) #Slicing [Extracting characters from middle of the string to the specified no of characters by defining last +1 character index ] using print() function
and
>>> m[8:11] # above Example without print() function
'and'
>>>
>>> m[0:4] #Slicing [Extracting first 4 Characters]
'Meet'
>>> m[6:15] #Slicing [Extracting characters from middle of the string till end]
'Choudhary'
>>>#Above two statements using print() function
>>> print(m[0:4])
Meet
>>> print(m[6:15])
Choudhary
>>> m='Gaurav Nanda' #resetting variable m value
>>> print(m[8:11]) #Slicing [Extracting characters from middle of the string to the specified no of characters by defining last +1 character index ] using print() function
and
>>> m[8:11] # above Example without print() function
'and'
>>>
Important Learning:
See in the above example how the starting index is always included, and the end index is always excluded. This is to makes sure that s[:i] + s[i:] is always equal to s:
Interesting Facts:
Python slice indices can be used with its useful default values; an omitted or null first index implies its default value which is zero, an omitted or null second index has its default value to the size of the string being sliced. See Example No 7Example 7:
>>> m='Gaurav Nanda' #Setting Variable Value
>>> m[:6] # Omitted start index
'Gaurav'
>>> m[7:] #Omitted last index
'Nanda'
# Same example using print() function
>>> print(m[:6])
Gaurav
>>> print(m[7:])
Nanda
>>>
>>> m[:6] # Omitted start index
'Gaurav'
>>> m[7:] #Omitted last index
'Nanda'
# Same example using print() function
>>> print(m[:6])
Gaurav
>>> print(m[7:])
Nanda
>>>
Note:
To check the length of the string we can use a Build-in function i.e. len See Example no 8.
Example 8:
>>> m="Gaurav Nanda" # Setting Variable value
>>> len(m)# Using build in function len
>>>
>>> len(m)
>>>
No comments:
Post a Comment