Strings in Python
Count the number of characters in a given string
Program:
text="hello how are you?"
print(len(text))
Output:
18
Count the number of words in a given string
Program:
#split function is used to splits a string into list.
text="hello how are you?"
print(len(text.split()))
Output:
4
Print Unique words from the given string
print(set(text.split()))
Print Unique words from the given string
print(len(set(text.split())))
Calculation of Lexical Diversity
noofwords=len(text.split())
noofuwords=len(set(text.split()))
ld=noofwords/noofuwords
print("Lexical Diversity(LD)= ",ld)
Count the occurrences of given word in a given string
Program:
Print the first 10 words of a given sentence
Program:
print(text.split()[0:10])
Print the words starts with a given letter
Program:
words=text.split()
for s in words:
if s.startswith('h'):
print(s)
how
Print the words ends with a given letter
Program:
words=text.split()
for s in words:
if s.endswith('e'):
print(s)
where
are
are
threre
Change the first letter of 10 words in Capital case and store them as a list
Program:
words=text.split()[0:10]
l=list()
for s in words:
l.append(s.title())
print(l)
how to split sentences from the paragraph
Program:
text="hello how are you? where are you? are you there?\napple is a red color\nsky is in blue color\n"
result = list(filter(lambda x : x != '', text.split('\n\n')))
for i in result:
print(i)
Output:
hello how are you? where are you? are you there?
apple is a red color
sky is in blue color
Removing \n from the given sentence
Program:
text="hello how are you\nwhere are you\nareyou going there\n"
st=""
for word in text:
st=st+word.replace("\n", "")
print(st)
Output:
hello how are youwhere are youareyou going there
Print first word and also print even positioned words
Program:
text="hello how are you where are you are you going there"
print(text.split()[0])
print(text.split()[0::2])
Output:
hello
['hello', 'are', 'where', 'you', 'you', 'there']
Removing digits from the given string
Program:
st="J1o2h3i4t5h6J7a8h9n1a2v3i4"
res = ' '.join([i for i in st if not i.isdigit()])
print(res)
Output:
JohithJahnavi
Reflective assignments require students to connect personal experiences with academic theories and demonstrate genuine learning. A sample reflective journal assignment expert can help students understand how to organize reflections, develop critical thinking, and present ideas clearly. Looking at suitable examples can make the writing process less confusing, especially when students are unsure about structure. It also provides a useful reference for developing a more thoughtful and organized approach to reflective academic writing.
ReplyDelete