0% found this document useful (0 votes)
14 views

assignment-02 - Python Strings

The document provides assignment instructions for students, encouraging them to attempt all questions and seek help during mentoring sessions. It includes a programming exercise involving text analysis, such as counting characters and words, finding lexical diversity, and manipulating strings. Additionally, it outlines various tasks related to the provided text about the history of SMS technology.

Uploaded by

Mbg Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

assignment-02 - Python Strings

The document provides assignment instructions for students, encouraging them to attempt all questions and seek help during mentoring sessions. It includes a programming exercise involving text analysis, such as counting characters and words, finding lexical diversity, and manipulating strings. Additionally, it outlines various tasks related to the provided text about the history of SMS technology.

Uploaded by

Mbg Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Assignment Instructions

Hello Innominion,

Try to attempt all the questions in every possible way.


Some other topics are required to solve some questions. don't panic.
Those questions can be answered after the topics are taught.

Join Mentoring Session for the Support/Doubts Resolving with Our Technical
Mentors (2.00 PM - 6.00 PM Mon-Sat)

Happy Learning !!!

In [ ]:

Strings - Execrise
In [40]: text = """The University of Hawaii began using radio to send digital information as earl
Friedhelm Hillebrand conceptualised SMS in 1984 while working for Deutsche Telekom. Sitt
Hillebrand typed out random sentences and counted every letter, number, punctuation, and
Almost every time, the messages contained fewer than 160 characters, thus giving the bas
limit one could type via text messaging. With Bernard Ghillebaert of France Télécom, he
a proposal for the GSM (Groupe Spécial Mobile) meeting in February 1985 in Oslo.
The first technical solution evolved in a GSM subgroup under the leadership of Finn Tros
It was further developed under the leadership of Kevin Holley and Ian Harris (see Short
SMS forms an integral part of SS7 (Signalling System No. 7). Under SS7, it is a "state" w
coded in the ITU-T "T.56" text format, that has a "sequence lead in" to determine differ
and may have special character codes that permits, for example, sending simple graphs as
This was part of ISDN (Integrated Services Digital Network) and since GSM is based on th
made its way to the mobile phone. Messages could be sent and received on ISDN phones,
and these can send SMS to any GSM phone. The possibility of doing something is one thing
implementing it another, but systems existed from 1988 that sent SMS messages to mobile

Question: How many characters in text

In [3]: len(text)

1507
Out[3]:

Question: How many words are there in the "text"

In [4]: len(text.split())

244
Out[4]:

Question: How many unique words in the "text"

In [3]: a=text.split()
b=set(a)
len(b)
174
Out[3]:

Question: Find Lexical diversity

lexical_diversity = (number of words)/(number of unique words)

In [6]: number_of_words=244
number_of_unique_words=174
lexical_diversity=(number_of_words)/(number_of_unique_words)
lexical_diversity

1.4022988505747127
Out[6]:

Question: Count how many "the" in text

In [2]: text1=text
text1.lower().count("the")

14
Out[2]:

Question: Count how many "a" in text

In [3]: text1=text
text1.lower().count("a")

92
Out[3]:

Question: Extract First 10 words in text

In [5]: " ".join(a[:10])

'The University of Hawaii began using radio to send digital'


Out[5]:

Question: Append " Innomatics Reseach Labs" after first 10 words in the text

In [6]: c=a[:10]
c.append(" Innomatics Reseach Labs")
" ".join(c)

'The University of Hawaii began using radio to send digital Innomatics Reseach Labs'
Out[6]:

Question: Extract First Fourteen (14) character in text

In [15]: text[0:15]

'The University '


Out[15]:

Question: Extract first Fourteen (14) words in text


In [7]: c=a[:14]
" ".join(c)

'The University of Hawaii began using radio to send digital information as early as'
Out[7]:

Question: Extract First 10 words in text

Convert every letter into upper case


Convert every letter into lower case

In [4]: a=text.upper()
b=a.split()
" ".join(b[0:10])

'THE UNIVERSITY OF HAWAII BEGAN USING RADIO TO SEND DIGITAL'


Out[4]:

In [7]: a=text.lower()
b=a.split()
" ".join(b[0:10])

'the university of hawaii began using radio to send digital'


Out[7]:

Question: Find the list of letter starting with

u
o hint - You need to use for loop for this

In [8]: a=text.lower()
b=a.split(" ")
abc = set()
for i in b:
if i.lower() and i.startswith('u'):
abc.add(i)
print("List of words starts with u is:")
print(abc)

List of words starts with u is:


{'under', 'using', 'university'}

In [9]: a=text.lower()
b=a.split(" ")
abc = set()
for i in b:
if i.lower() and i.startswith('o'):
abc.add(i)
print("List of words starts with o is:")
print(abc)

List of words starts with o is:


{'of', 'oslo.', 'one', 'out', 'on'}

In [27]:

List of words staring with o is:


{'out', 'of', 'one', 'on', 'oslo.'}

Question: Find the list of letter ending with

e
n hint - You need to use for loop for this

In [10]: a=text.lower()
b=a.split(" ")
abc = set()
for i in b:
if i.lower() and i.endswith('e'):
abc.add(i)
print("List of words ends with e is:")
print(abc)

List of words ends with e is:


{'while', 'these', 'france', '(see', 'one', 'message', 'determine', 'since', '(compare',
'\nthe', 'simple', 'mobile', 'deutsche', 'have', 'he', '\nmade', 'be', 'the', 'languag
e', 'type', '(groupe', '"sequence'}

In [11]: a=text.lower()
b=a.split(" ")
abc = set()
for i in b:
if i.lower() and i.endswith('n'):
abc.add(i)
print("List of words ends with n is:")
print(abc)

List of words ends with n is:


{'finn', 'can', 'began', 'information', 'in', 'solution', 'kevin', 'an', 'isdn', 'ian',
'on', 'than'}

Question: Extract first 10 words of text and Capitalize first letter of each word

In [12]: a = text.split(" ")


" ".join(a[0:10]).title()

'The University Of Hawaii Began Using Radio To Send Digital'


Out[12]:

Question: Replace the word "University" with name "Innomatics" in text

In [13]: a = text.replace("University","Innomatics")
a

'The Innomatics of Hawaii began using radio to send digital information as early as 197
Out[13]:
1,using ALOHAnet. \nFriedhelm Hillebrand conceptualised SMS in 1984 while working for De
utsche Telekom. Sitting at a typewriter at home, \nHillebrand typed out random sentences
and counted every letter, number, punctuation, and space. \nAlmost every time, the messa
ges contained fewer than 160 characters, thus giving the basis for the \nlimit one could
type via text messaging. With Bernard Ghillebaert of France Télécom, he developed \na pr
oposal for the GSM (Groupe Spécial Mobile) meeting in February 1985 in Oslo. \nThe first
technical solution evolved in a GSM subgroup under the leadership of Finn Trosby. \nIt w
as further developed under the leadership of Kevin Holley and Ian Harris (see Short Mess
age Service). \nSMS forms an integral part of SS7 (Signalling System No. 7). Under SS7,
it is a "state" with a 160 character data, \ncoded in the ITU-T "T.56" text format, that
has a "sequence lead in" to determine different language codes, \nand may have special c
haracter codes that permits, for example, sending simple graphs as text. \nThis was part
of ISDN (Integrated Services Digital Network) and since GSM is based on this, \nmade its
way to the mobile phone. Messages could be sent and received on ISDN phones, \nand these
can send SMS to any GSM phone. The possibility of doing something is one thing, \nimplem
enting it another, but systems existed from 1988 that sent SMS messages to mobile phones
(compare ND-NOTIS).'
Question: Convert the "text" into sentances and store those into one variable called
"sentance"

In [14]: sentance= text.split(".")


sentance

['The University of Hawaii began using radio to send digital information as early as 197
Out[14]:
1,using ALOHAnet',
' \nFriedhelm Hillebrand conceptualised SMS in 1984 while working for Deutsche Teleko
m',
' Sitting at a typewriter at home, \nHillebrand typed out random sentences and counted
every letter, number, punctuation, and space',
' \nAlmost every time, the messages contained fewer than 160 characters, thus giving th
e basis for the \nlimit one could type via text messaging',
' With Bernard Ghillebaert of France Télécom, he developed \na proposal for the GSM (Gr
oupe Spécial Mobile) meeting in February 1985 in Oslo',
' \nThe first technical solution evolved in a GSM subgroup under the leadership of Finn
Trosby',
' \nIt was further developed under the leadership of Kevin Holley and Ian Harris (see S
hort Message Service)',
' \nSMS forms an integral part of SS7 (Signalling System No',
' 7)',
' Under SS7, it is a "state" with a 160 character data, \ncoded in the ITU-T "T',
'56" text format, that has a "sequence lead in" to determine different language codes,
\nand may have special character codes that permits, for example, sending simple graphs
as text',
' \nThis was part of ISDN (Integrated Services Digital Network) and since GSM is based
on this, \nmade its way to the mobile phone',
' Messages could be sent and received on ISDN phones, \nand these can send SMS to any G
SM phone',
' The possibility of doing something is one thing, \nimplementing it another, but syste
ms existed from 1988 that sent SMS messages to mobile phones (compare ND-NOTIS)',
'']

Question:

1.From the above sentance remove '\n'

In [41]: a=text.replace("\n"," ")


a

'The University of Hawaii began using radio to send digital information as early as 197
Out[41]:
1,using ALOHAnet. Friedhelm Hillebrand conceptualised SMS in 1984 while working for Deu
tsche Telekom. Sitting at a typewriter at home, Hillebrand typed out random sentences a
nd counted every letter, number, punctuation, and space. Almost every time, the message
s contained fewer than 160 characters, thus giving the basis for the limit one could ty
pe via text messaging. With Bernard Ghillebaert of France Télécom, he developed a propo
sal for the GSM (Groupe Spécial Mobile) meeting in February 1985 in Oslo. The first tec
hnical solution evolved in a GSM subgroup under the leadership of Finn Trosby. It was f
urther developed under the leadership of Kevin Holley and Ian Harris (see Short Message
Service). SMS forms an integral part of SS7 (Signalling System No. 7). Under SS7, it is
a "state" with a 160 character data, coded in the ITU-T "T.56" text format, that has a
"sequence lead in" to determine different language codes, and may have special characte
r codes that permits, for example, sending simple graphs as text. This was part of ISDN
(Integrated Services Digital Network) and since GSM is based on this, made its way to t
he mobile phone. Messages could be sent and received on ISDN phones, and these can send
SMS to any GSM phone. The possibility of doing something is one thing, implementing it
another, but systems existed from 1988 that sent SMS messages to mobile phones (compare
ND-NOTIS).'

1. From the above sentace print the first word from each sentance
In [42]: b = text.split(".")
for i in b[:len(b)-1]:
res = i.split()
print(res[0])

The
Friedhelm
Sitting
Almost
With
The
It
SMS
7)
Under
56"
This
Messages
The

1. From the above sentace print even posioned sentances

In [35]: b = text.split(".")
for i in b[:len(b):2]:
res = i
print(res)

The University of Hawaii began using radio to send digital information as early as 1971,
using ALOHAnet
Sitting at a typewriter at home,
Hillebrand typed out random sentences and counted every letter, number, punctuation, and
space
With Bernard Ghillebaert of France Télécom, he developed
a proposal for the GSM (Groupe Spécial Mobile) meeting in February 1985 in Oslo

It was further developed under the leadership of Kevin Holley and Ian Harris (see Short
Message Service)
7)
56" text format, that has a "sequence lead in" to determine different language codes,
and may have special character codes that permits, for example, sending simple graphs as
text
Messages could be sent and received on ISDN phones,
and these can send SMS to any GSM phone

Question: If the following string is given as input to the program:

H1e2l3l4o5w6o7r8l9d

Output: Then, the output of the program should be:

Helloworld

In [43]: a="H1e2l3l4o5w6o7r8l9d"
a[: :2]

'Helloworld'
Out[43]:

Innomatics Research Labs


www.innomatics.in

In [ ]:

You might also like