0% found this document useful (0 votes)
17 views3 pages

string programs

The document contains Python code snippets for various string manipulation tasks, including reversing a string, checking for palindromes, counting vowels, and finding the longest word in a sentence. It also includes functions to remove duplicates, check for anagrams, and count occurrences of a substring. Overall, it serves as a collection of basic string processing functions in Python.

Uploaded by

prr technologies
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

string programs

The document contains Python code snippets for various string manipulation tasks, including reversing a string, checking for palindromes, counting vowels, and finding the longest word in a sentence. It also includes functions to remove duplicates, check for anagrams, and count occurrences of a substring. Overall, it serves as a collection of basic string processing functions in Python.

Uploaded by

prr technologies
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 3
4124, 158 PM in (a): out 2] 1 [2] out (2): mn [3]: out (3) in [4]: Uniited401 -Jupyter Notebook # write a Python program to reverse a string def reverse_string(input_string): return input_string[?:-1] reverse_string(”PRR") Rae" # Write a Python program to check if a string is a palindrome. ‘def is_palindrone(input_string) : return input_string == input_string[::-1] 4s_palindrane(*221") True # write a Python progran to count the number of vowels in a string. def count_vowels(input_string): vowels = "aeiouAFTOU" return sun(i for char in input_string if char in vowels) count_vowels( Apple") # count vouels in a string # declare, assign string str = "Hello world” # declare count count = @ # iterate and check each character for i in str: # check the conditions for vowels af ( a wie nie oo oon count += 2 # print count print(*Total vowels are: ", count) Total vowels are: 3 localhost B888/notebooks/Untiled401 ipynb 18 4124, 158 PM mn [5]: In [6]: outs]: In [a]: outta]: mn (2) out (2): Uniited401 - Jupyter Notebook 4 Print EVEN Length words # print EVEN Length words of @ string # declare, assign string str = “Python is a programming language” # extract words in List words = list(str.split(” ")) 4# print string print(*str: ", str) 4 print List converted string i.e, List of words print("List converted string: ", words) # iterate words, get Length # §f Length ts EVEN print word print (“EVEN Tength words:") ‘for W in word: Af Len(h) X 2 == 8: print(a) str: Python is 2 programming language List converted string: ['Python’, ‘is', EVEN length words: Python Language “progranming’, ‘Language’ ] 4 write a Python program to remove all duplicates from a string and return the result. def renove_duplicates(input_string): return "".join(sorced(set(input_stning), key=input_string.index)) renove_duplicates( banana’) “ban* # Write a Python progran to find the Longest word in a sentence. def Longest_word(input_string) words = input_string.split() return max(words, key=len) Longest _word(‘Python is a programming Language’ ) " programming’ # Write a Python program to check if tio strings are anagrams. def are_anagrans(strt, str2) return sorted(stri) == sorted(str2) are_anagrans(’PRR', RPR") True localhost B888/notebooks/Untiled401 ipynb 208 4124, 158 PM Uniited401 - Jupyter Notebook In [3]: rite @ Python program to count the number of occurrences of @ specific substring in a string. count_substring_occurrences(input_string, substring): count = & index = @ hile True: index = input_string.find(substring, index) Af index break count #= 1 index 4= 2 return count lunt_substring_occurrences( ‘python is a progranming, python used to develop many applications" ‘python « » out (3): 2 In [4]: |# Write a Python progran to find the Length of the Last word in a sentence, def Length_of_last_word(input_string): words = input_string.spli@() if word return len(words{-1]) retuen @ Length_of_last_word(’P) 1on is 2 progranming Language’) out(a]: 8 In [5]: |# Write a Python program to reverse the order of words in a sentence. def reverse_words_order(input_string) words = input_string.split() return " ".Join(reversed(words)) reverse _words_order( Py 1on is 2 progranming Language’) out (5): ‘Language progranming a is Python’ Intl localhost B888/notebooks/Untiled401 ipynb 38

You might also like