Professional Documents
Culture Documents
STRING
LESSON - ONE When triple single and triple double quotes are
used?
INTRODUCTION TO STRING
If you want to create multiple lines of string, then
We already learn the first Hello World program in triple single or triple double quotes are the best to
python. In that program we just print a group of use.
characters by using print() function. Those groups of
Ex 9.1 ( Creating a string using four ways )
characters are called as a string.
name = 'Michael Jackson '
Example: Python Program to print string
music = "Michael’s song "
print("Hello World")
s = '"Michael’s song is awesome."'
Hello World
print("My name is ",name)
What is a string?
print("This is ",music)
A group of characters enclosed within single or
double or triple quotes is called as string. We can say print("{} Thomas D.".format(s))
string is a sequential collection of characters. My name is Michael Jackson
How to create a String? This is Michael’s song
There are four ways to create a string. "Michael’s song is awesome." Thomas D.
PS. variable “s” stands for string name. How to create an empty string?
Syntaxs : If a string has no characters in it then it is called an
s = ‘String’ #single quotes empty string.
Note: Generally, to create a string mostly used syntax This is empty string
is double quotes syntax.
STRING 48
LESSON - TWO M
J
ACCESSING A STRING
Ex 9.4 ( Accessing a string with float index )
How to access a String?
name = "Michael Jackson"
There are three ways in which you can access the
characters in the string. They are: print(name[1.3])
By using indexing TypeError: string indices must be
By using slicing operator integers
By using loops ( PS. we will discuss in later
Control Flow Chapter ) Ex 9.5 ( string with out of bound index )
Positive indexing: The position of string What if we want to access a word from a string?
characters can be a positive index from left In such scenarios we use string slicing.
to right direction (we can say forward
direction). In this way, the starting position What is Slicing?
is 0 (zero).
A substring of a string is called a slice. A slice can
Negative indexing: The position of string represent a part of string from string or a piece of
characters can be negative indexes from string. String slicing result is string type. We need to
right to left direction (we can say backward use square brackets [] in slicing.
direction). In this way, the starting position
is -1 (minus one). In slicing we will not get any Index out of range
exception. In slicing indices should be integer or
Diagram representation None otherwise we will get errors.
Syntax: s[start:stop:step]
start: Represent from the starting index position
(default is 0)
Note: If we are trying to access characters of a string stop: Represents the (ending index-1) position
with out of range index, then we will get error as (default is last index)
IndexError.
step: Represents the increment the position of the
Ex 9.3 ( Accessing a string with index ) index while accessing (default is 1)
name = "Michael Jackson" Note: If you are not specifying the begin index, then
print(name[0]) it will consider the beginning of the string. If you are
not specifying the end index, then it will consider the
print(name[-7]) end of the string. The default value for step is 1.
STRING 49
Ex 9.15 ( Comparison operators on string ) Note: These methods won’t remove the spaces which
/ User name validation / are in the middle of the string.
A space is also considered as a character inside We can find a substring in two directions like
a string. Sometimes unnecessary spaces in a string forward and backward direction. We can use the
will lead to wrong results. following 4 methods:
un = "michael" find()
index()
i = str(input("Enter user name:"))
For backward direction
if i == un:
rfind()
print("Hello", i) rindex()
print("Welcome to facebook.")
❺ find() method
else:
This method returns an index of the first occurrence
print("Invalid user name!") of the given substring.
Enter user name: mic_hael_ If it is not available, then we will get -1(minus one)
value. By default find() method can search the total
Invalid username! string of the object.
PS. Here _ means/indicates space not underscore. Syntax – s.find(substring)
❹ strip() You can also specify the boundaries while searching.
Predefined methods to remove spaces in Python Syntax – s.find(substring, begin, end)
rstrip() - remove spaces at right side of PS. It will always search from begin index to
string end - 1 index.
lstrip() - remove spaces at left side of string
strip() - remove spaces at left & right sides
of string
STRING 52
Ex 9.18 ( finding substring by using find method ) What if we want to find total no. of a specific
fn = input("Enter your name: ") letter or word in a string?
print(fn.upper()) Rules:
Minimum 5 characters
print(fn.lower()) Maximum 10 characters
print(fn.swapcase()) Only lowercase letters
Underscore and dot is allowed
print(fn.title()) Whitespace, symbol are not allowed
print(fn.capitalize())
Finally What about Character data type?
MICHAEL JACKSON
If a single character stands alone then we can say that
michael jackson is a single character. For example: M for Male F for
mIChAeL JacKSon Female.
else:
print('No Lowercase')
del p
else:
print('Less Characters')