Home | | Computer Science 12th Std | Programs using Strings

Python - Programs using Strings | 12th Computer Science : Chapter 8 : Core Python : Strings and String Manipulation

Chapter: 12th Computer Science : Chapter 8 : Core Python : Strings and String Manipulation

Programs using Strings

Core Python : Strings and String Manipulation : Programs using Strings

Programs using Strings :

 

Program to check whether the given string is palindrome or not

str1 = input ("Enter a string: ")

str2 = ' '

index=-1

for i in str1:

str2 += str1[index]

index -= 1

print ("The given string = { } \n The Reversed string = { }".format(str1, str2))

if (str1==str2):

print ("Hence, the given string is Palindrome")

else:

print ("Hence, the given is not a palindrome")

Output : 1

Enter a string: malayalam

The given string = malayalam

The Reversed string = malayalam

Hence, the given string is Palindrome

Output : 2

Enter a string: welcome

The given string = welcome

The Reversed string = emoclew

Hence, the given string is not a palindrome

 

Program to display the following pattern

*

* *

* * *

* * * *

* * * * *

str1=' * '

i=1

while i<=5:

print (str1*i)

i+=1

Output

*

* *

* * *

* * * *

* * * * *

 

Program to display the number of vowels and consonants in the given string

str1=input ("Enter a string: ")

str2="aAeEiIoOuU"

v,c=0,0

for i in str1:

if i in str2:

v+=1

else:

c+=1

print ("The given string contains { } vowels and { } consonants".format(v,c))

Output

Enter a string: Tamilnadu School Education

The given string contains 11 vowels and 15 consonants

 

Program to create an Abecedarian series. (Abecedarian refers list of elements appear in alphabetical order)

str1="ABCDEFGH"

str2="ate"

for i in str1:

print ((i+str2),end='\t')

Output

Aate  Bate  Cate  Date  Eate   Fate   Gate  Hate

 

Program that accept a string from the user and display the same after removing vowels from it

def rem_vowels(s):

temp_str=''

for i in s:

if i in "aAeEiIoOuU":

pass

else:

temp_str+=i

print ("The string without vowels: ", temp_str)

str1= input ("Enter a String: ")

rem_vowels (str1)

Output

Enter a String: Mathematical fundations of Computer Science

The string without vowels: Mthmtcl fndtns f Cmptr Scnc

 

Program that count the occurrences of a character in a string

def count(s, c):

c1=0

for i in s:

if i == c:

c1+=1

return c1

str1=input ("Enter a String: ")

ch=input ("Enter a character to be searched: ")

cnt=count (str1, ch)

print ("The given character {} is occurs {} times in the given string".format(ch,cnt))

Out Put

Enter a String: Software Engineering

Enter a character to be searched: e

The given character e is occurs 3 times in the given string

 

Tags : Python , 12th Computer Science : Chapter 8 : Core Python : Strings and String Manipulation
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
12th Computer Science : Chapter 8 : Core Python : Strings and String Manipulation : Programs using Strings | Python


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.