12 Lecture 12 Python Strings
12 Lecture 12 Python Strings
for i in msg1:
print(i)
or
for i in range(0,len(msg1)):
print(msg1[i])
Accessing Substrings in Strings
• The slice operator or square brackets [] is used to access the individual characters
of the string.
• However, we can use the : (colon) operator in Python to access the substring from
the given string.
string[start:end:step]
where,
start: The starting index of the substring. The character at this index is
included in the substring. If start is not included, it is assumed to equal to 0.
end: The terminating index of the substring. The character at this index is not
included in the substring. If end is not included, or if the specified value
exceeds the string length, it is assumed to be equal to the length of the
string by default.
step: Every "step" character after the current character to be included. The
default value is 1. If step is not included, it is assumed to be equal to 1.
Accessing Substrings in Strings
string[start:end] Get all characters from start to end – 1
string[:end] Get all characters from the beginning of the string to end - 1
string[start:] Get all characters from start to the end of the string
Note:
The start or end index can be a negative number.
A negative index means that you start counting from the end of the string
instead of the beginning (from the right to left).
Accessing Substrings in Strings
msg2= "Python Programming“
msg2[1:5] #this will give substring starting from index 1 to (5-1)
s=“NIT SRINAGAR”
del s will delete the string object “NIT Srinagar”
print(s) # will give error
We cannot delete individual characters of string. i.e. del s[0] will not work
String Operators
Operator Description
It is known as concatenation operator used to join the strings given either side of the
+
operator.
It is known as repetition operator. It concatenates the multiple copies of the same
*
string.
[] It is known as slice operator. It is used to access an element from a particular string.
It is known as range slice operator. It is used to access the substring from the specified
[:]
range.
It is known as membership operator. It returns if a particular sub-string is present in the
in
specified string.
It is also a membership operator and does the exact reverse of in. It returns true if a
not in
particular substring is not present in the specified string.
It is used to specify the raw string. Raw strings are used in the cases where we need to
r/R print the actual meaning of escape characters such as "C://python". To define any string
as a raw string, the character r or R is followed by the string.
String Methods
• A method is like a function, but it runs "on" an object.
Suppose s=“NiT Srinagar”
1. s.lower(): returns the lowercase version of the string
2. s.upper(): returns the uppercase version of the string
3. s.split(): method splits a string into a list by using whitespace as
separator. For example: s.split will return a list with two strings
“NIT” and “Srinagar”. We can split the strings using some other
separator also.
s.split(‘a’)
String Methods
4. s.count(): Returns the number of times a specified value occurs
in a string
5. s.find(“nit”): searches for the given string “nit” (not a regular
expression) within s, and returns the first index where it begins or -1 if
not found
6. s.replace('old', 'new') -- returns a string where all occurrences of
'old' have been replaced by 'new'
S=“NIT Srinagar”