- String is the most commonly used object in any project and in any programming language. Hence we should aware complete information about String data type.
- Any sequence of characters within either single quotes or double quotes is considered as a String.
Syntax
Python
s = 'Python'
s = "Python"
Note
- In most of other languages like C, C++, Java, a single character with in single quotes is treated as char data type value. But in Python we are not having char data type. Hence it is treated as String only.
Example
Python
ch='a'
print(type(ch))
Output
PowerShell
<class 'str'>
- We can define multi-line String literals by using triple single or double quotes.
Example
Python
s='''internet
knowledge
hub'''
print(type(s))
# OR
s="""internet
knowledge
hub"""
print(type(s))
Output
PowerShell
<class 'str'>
<class 'str'>
Accessing Characters of a String
- We can access characters of a string by using the following ways.
- Using index
- Using slice operator
Using index
- Python supports both +ve and -ve index.
- +ve index means left to right (Forward direction).
- -ve index means right to left (Backward direction).
Example
Python
s='amit'
print(s[0])
print(s[3])
print(s[-1])
print(s[10])
print(s[-10])
Output
PowerShell
'a'
't'
't'
IndexError: string index out of range
IndexError: string index out of range
Note
- If we are trying to access characters of a string with out of range index then we will get error saying : IndexError
Question
Write a program to accept some string from the keyboard and display its characters by index wise (both positive and negative index)
Python
s=input("Enter Some String:")
for i in range(len(s)):
print("The character present at positive index {} and at nEgative index {} is {}".format(i,i-len(s),s[i]))
Output
PowerShell
Enter Some String:ajay shukla
The character present at positive index 0 and at nEgative index -11 is a
The character present at positive index 1 and at nEgative index -10 is j
The character present at positive index 2 and at nEgative index -9 is a
The character present at positive index 3 and at nEgative index -8 is y
The character present at positive index 4 and at nEgative index -7 is
The character present at positive index 5 and at nEgative index -6 is s
The character present at positive index 6 and at nEgative index -5 is h
The character present at positive index 7 and at nEgative index -4 is u
The character present at positive index 8 and at nEgative index -3 is k
The character present at positive index 9 and at nEgative index -2 is l
The character present at positive index 10 and at nEgative index -1 is a
Using slice operator
Syntax
Python
str[begin Index : end Index : step]
# begin Index: From where we have to consider slice (substring).
# end Index: We have to terminate the slice (substring) at end Index-1
# step: incremented value
Note
- If we are not specifying begin Index then it will consider from beginning of the string.
- If we are not specifying end index then it will consider up to end of the string.
- The default value for step is 1.
Example
Python
s="Learning Python is very very easy!!!"
print(s[1:7:1])
print(s[1:7])
print(s[1:7:2])
print(s[:7])
print(s[7:])
print(s[::])
print(s[:])
print(s[::-1])
Output
PowerShell
earnin
earnin
eri
Learnin
g Python is very very easy!!!
Learning Python is very very easy!!!
Learning Python is very very easy!!!
!!!ysae yrev yrev si nohtyP gninraeL
Remark
- Step value can be either +ve or –ve.
- if +ve then it should be forward direction (left to right).
- if -ve then it should be backward direction (right to left).
- Either forward or backward direction, we can take both +ve and -ve values for begin Index and end Index.
Ungraded Questions
Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!