Python Strings
Python Strings
TERM - I
Assignment:
Name='Amar Shah'
City="Hyderabad"
Place="King's Palace"
Title='The Dream of "Doll" uncovered'
print(Name,"Lived in the", Place,"at", City)
print("His best book was entitled as",Title)
Amar Shah Lived in the King's Palace at Hyderabad
His best book was entitled as The Dream of "Doll" uncovered
String - Traversal 5
Txt="PEACE4LIFE"
T1="STOP"
Msg="Hello"
count() method searches the substring in a given string and returns how many
It also takes two optional parameters start and end to specify the starting and
ending positions within the string.
<String>.count(<Substring>,<Start>,<End+1>)
TXT="BELIEVE IN SELF"
find() method returns the index of first occurrence of a substring inside a string if
It also takes two optional parameters start and end to specify the starting and
ending positions within the string.
<String>.find(<Substring>,<Start>,<End+1>)
TXT="BELIEVE IN SELF"
index() method returns the index of the first occurrence of a substring inside a
It also takes two optional parameters start and end to specify the starting and
ending positions within the string.
<String>.index(<Substring>,<Start>,<End+1>)
index() find()
Returns the index of the first Returns the index of the first
occurrence of a substring inside a occurrence of a substring inside a
string, if it is found. string if it is found.
If the index is not found, it raises If the index is not found, it returns
an exception (error). -1.
startswith() 18
Methods
T1="Num 23";T2="car24"
T3="AMAZING";T4="end"
Join all items in a string, a tuple or a list into a string, using a separator string.
OUTPUT
Code="MLYLM" Coded Message: MLYLM
Decode ="A".join(Code) Decoded Message:
print("Coded Message:",Code) MALAYALAM
print("Decoded Message:",Decode)
String - join() [Example 2] 23
Join all items in a string, a tuple or a list into a string, using a separator string.
OUTPUT
FL="HD" FIRST LAST: HD
NM ="YDERABA".join(FL) FULL NAME : HYDERABAD
print("FIRST LAST:",FL)
print("FULL NAME :",NM)
String - join() [Example 3] 24
Join all items in a string, a tuple or a list into a string, using a separator string.
OUTPUT
FRIENDS=("AMAR","AKBAR","ANTHONY") AMAR & AKBAR & ANTHONY
TOGETHER=" & ".join(FRIENDS)
print(TOGETHER)
tuple data type will be discussed in detail later. At present, you just need to know that it is
comma separated collection of elements (of any type) enclosed inside set of round braces ()
String - join() [Example 4] 25
Join all items in a string, a tuple or a list into a string, using a separator string.
OUTPUT
FRIENDS=["AMAR","AKBAR","ANTHONY"] AMAR & AKBAR & ANTHONY
TOGETHER=" & ".join(FRIENDS)
print(TOGETHER)
list data type will be discussed in detail later. At present, you just need to know that it is
comma separated collection of elements (of any type) enclosed inside set of square braces []
String - join() [Example 5] 26
Join all items in a string, a tuple or a list into a string, using a separator string.
NOTE: Please note all the items are required to be of the type string and it
returns string type only
list data type will be discussed in detail later. At present, you just need to know that it is comma
separated collection of elements (of any type) enclosed inside set of square braces []
String - split() 27
Method
<List>=<Str>.split(<Separator>[,<Maxsplit>])
Separator : String splits at this specified <Separator>. space is the default
separator.
T="BELIEVE IN SELF"
T="BELIEVE IN SELF"
Method
<Tuple>=<Str>.partition(<Partition String>)
Partition String : A string that will partition the Str into three parts in a tuple.
String - partition() - Example 1 32
T="BELIEVE IN SELF"
OUTPUT
OUTPUT
P="PEACE ON EARTH"
Methods
<Var>=<str>.strip(<Character>)
String - strip(), lstrip(), rstrip() 35
Method
<New Str>=<Str>.replace(<Old>,<New>,[<Count>])
String - replace() - Example 1 37
T="BELIEVE IN SELF"
S1="20 IS MINE"
Default Values - Start Index (Beginning of string) End Index (End of string)
<String>[::<+ve step>]
Default Values - Start Index (End of string) End Index (Beginning of string)
<String>[::<-ve Step>]
String - Slicing 40
OUTPUT
T="BELIEVE IN SELF"
N="0123456789"
OUTPUT
0 1 2 3 4 5 6 7 8 9
N 0 1 2 3 4 5 6 7 8 9
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
To do - “More with strings” 42
● To count and display “No. of times” of presence of words “the” and “to” in a