Chap-2] String Handling, Classes, Modules and Package
Strings in Python: -
“Python string is the collection of the characters surrounded by single quotes, double
quotes, or triple quotes. “
Consider the following example in Python to create a string.
Syntax:
str = "Hi Python !"
In Python, strings are treated as the sequence of characters, which means that Python
doesn't support the character data-type.
A single character written as 'p' is treated as the string of length 1.
Creating String in Python
We can create a string by enclosing the characters in single-quotes or double-
quotes.
Python also provides triple-quotes to represent the string, but it is generally used
for multiline string or docstrings.
Example:
1. #Using single quotes
2. str1 = 'Hello Python'
3. print(str1)
4. #Using double quotes
5. str2 = "Hello Python"
6. print(str2)
7. #Using triple quotes
8. str3 = '''''Triple quotes are generally used for
9. represent the multiline or
10. docstring'''
11. print(str3)
Mr. Dhanraj Suresh Kiwde Page 1
Output:
Hello Python
Hello Python
Triple quotes are generally used for
represent the multiline or
docstring
String Operations: -
Python strings are "immutable" which means they cannot be
changed after they are created.
To create a string, put the sequence of characters inside either
single quotes, double quotes, or triple quotes and then assign it to
a variable.
Following are the some common string operations are:
1. String Concatenation
Joining of two or more strings into a single one is called
concatenation.
Python uses "+" operator for joining one or more strings.
Example:-
>>> str1='Hello'
>>> str2='World'
>>> print(str1+str2)
HelloWorld
2. Reverse a String
In Python Strings are sliceable.
Slicing a string gives you a new string from one point in the string,
backwards or forwards, to another point, by given increments.
They take slice notation or a slice object in a subscript:
Mr. Dhanraj Suresh Kiwde Page 2
string[subscript]
The subscript creates a slice by including a colon within the braces:
string[begin:end:step]
It works by doing [begin:end:step] - by leaving begin and end off
and specifying a step of -1, it reverses a string.
Example:
>>> str='Python String'
>>> print(str[::-1])
gnirtS nohtyP
String Methods
Python has several built-in methods associated with the string
data type.
These methods let us easily modify and manipulate strings.
Built-in methods are those that are defined in the Python
programming language and are readily available for us to use.
Here are some of the most common string methods.
1. Python String len() method
String len() method return the length of the string.
Example:
>>> str='Hello World!'
>>> print(len(str))
12
Mr. Dhanraj Suresh Kiwde Page 3
2. Python String count() method
String count() method returns the number of occurrences of a
substring in the given string.
Example:
>>> str='Python is object oriented'
>>> substr='object'
>>> print(str.count(substr)) #Return 1 ,because the word object exist 1 time in str.
1
3. Python String index() method
String index() method returns the index of a substring inside the
given string.
index(substr,start,end)
end(optional) by default its equal to the length of the string.
Example:
>>> str='Python is object oriented'
>>> substr='is'
>>> print(str.index(substr))
7
4. Python String upper() method
String upper() convert the given string into Uppercase letters and
return new string.
Example:
>>> str='Python is object oriented'
>>> print(str.upper())
PYTHON IS OBJECT ORIENTED
Mr. Dhanraj Suresh Kiwde Page 4
5. Python String lower() method
String lower() convert the given string into Lowercase letters and
return new string.
Example:
>>> str='PYTHON IS OBJECT ORIENTED'
>>> print(str.lower())
python is object oriented
6. Python String startswith() method
String startswith() method returns Boolean TRUE, if the string
Starts with the specified substring otherwise, it will return False.
Example:
>>> str='Python is object oriented'
>>> print(str.startswith('Python'))
True
>>> print(str.startswith("Object"))
False
7. Python String endswith() method
String endswith() method returns Boolean TRUE, if the string Ends
with the specified substring otherwise, it will return False.
Mr. Dhanraj Suresh Kiwde Page 5
Example:
>>> str='Python is object oriented'
>>> print(str.endswith('oriented'))
True
>>> print(str.endswith('Python'))
False
8. Python String split() method
String split() method break up a string into smaller strings based on
a delimiter or character.
Example:
>>> str='Python is object oriented'
>>> print(str.split())
['Python', 'is', 'object', 'oriented']
9. Python String join() method
String join() is a string method which returns a string concatenated
with the elements of an iterable.
Example:
>>> str='Python is object oriented'
>>> tmp='-'
>>> print(tmp.join(str))
P-y-t-h-o-n- -i-s- -o-b-j-e-c-t- -o-r-i-e-n-t-e-d
Mr. Dhanraj Suresh Kiwde Page 6
10. Python String find() method
String find() return the index position of the first occurrence of a
specified string.
It will return -1, if the specified string is not found.
Example:
>>> str='Python is object oriented'
>>> st='object'
>>> print(str.find(st))
10
>>> print(str.find(st,20))
-1
String Slicing: -
Slicing is the process of obtaining a portion (substring) of a string by
using its indices.
Given a string, we can use the following template to slice it and obtain a
substring:
string[start:end]
start is the index from where we want the substring to start.
end is the index where we want our substring to end.
The character at the end index in the string, will not be included in the
substring obtained through this method.
Here is a basic example of string slicing.
S = 'ABCDEFGHI'
print(S[2:7]) # C D E F G
Mr. Dhanraj Suresh Kiwde Page 7
Example:
>>> str='ABCDEFGHI'
>>> print(str[2:7])
CDEFG
Mr. Dhanraj Suresh Kiwde Page 8