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

Program:
text="hello how are you where are you are you going threre"
print(set(text.split()))

Output:
{'you', 'where', 'are', 'threre', 'how', 'hello', 'going'}  

Print Unique words from the given string

Program:
text="hello how are you where are you are you going threre"
print(len(set(text.split())))
 
 
Output:
7

Calculation of Lexical Diversity

One of the most commonly used approaches to measure LD is to use the ratio of unique lexical items divided by the total number of words in a sample  
Program
text="hello how are you where are you are you going threre"
noofwords=len(text.split())
noofuwords=len(set(text.split()))
ld=noofwords/noofuwords
print("Lexical Diversity(LD)= ",ld)
 
Output
Lexical Diversity(LD)=  1.5714285714285714 

Count the occurrences of given word in a given string

Program:

text="hello how are you where are you are you going threre"
print(text.count("are"))
 
Output:  
3
 

Print the first 10 words of a given sentence

Program:

text="hello how are you where are you are you going there"
print(text.split()[0:10])

 Output:  
 ['hello', 'how', 'are', 'you', 'where', 'are', 'you', 'are', 'you', 'going']


Print the words starts with a given letter

Program:

text="hello how are you where are you are you going threre"
words=text.split()
for s in words:
    if s.startswith('h'):
        print(s)


 Output:  
hello
how

Print the words ends with a given letter

Program:

text="hello how are you where are you are you going threre"
words=text.split()
for s in words:
    if s.endswith('e'):
        print(s)

 Output:  
are
where
are
are
threre

Change the first letter of 10 words in Capital case and store them as a list

Program:

text="hello how are you where are you are you going threre"
words=text.split()[0:10]
l=list()
for s in words:
   l.append(s.title())
print(l)

 Output:  
['Hello', 'How', 'Are', 'You', 'Where', 'Are', 'You', 'Are', 'You', 'Going']

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

Comments

  1. 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

Post a Comment

Popular posts from this blog

Tuples in Python

Exception Handling in Python