Blog Archive

search

Tuesday, August 6, 2019

Python program to strip a set of characters from a string


a = input(' enter an string : ')
b = input('enter charecters to strip : ')
c=''
d=a
for i in b:
    c=''
    for j in range(len(d)):
        if(i != d[j]):
            c = c+d[j]
    d=c   
print(c)
   

Python program to reverse words in a string

a = input(' enter a string ')
b = a.split(' ')
c = len(b)
d=''
for i in range(c):
    d = b[i] +' '+ d
print(d)

Python program to round off of a floating point number

x = 3.1415926
y = -12.9999
a="{:.0f}".format(x)
b="{:.0f}".format(y)
print(" Roundoff of ",x," = ",a)
print(" Roundoff of ",y," = ",b)

Python program to remove a newline and white space

a = input('Enter a string : ')
print(a)
print(len(a))
b=a.rstrip()
print(b)
print(len(b))

Python program to sort a string

a = input('Enter a string : ')
print(a)
b=sorted(a)
print(b)

Python program to reverses a string

a = input('Enter a string : ')
print(a)
b=a
b=b[::-1]
print(b)

Python program to split string using a specified character.

a = input('Enter Date : ')
print(a)
b=a.rsplit('-') #split using '-'
for i in b:
    print(' ',i)

Write a Python function to insert a string in the position of a string

a = input('Enter a string ')
b = input('enter a string to to insert')
c = int(input('enter a position'))
d=''
d=a[:c]+b+a[c:]
print(d)

Python program that accepts a comma separated sequence of words as input and prints the unique words in sorted form

a = input('Input comma separated sequence of words ')
words = [word for word in a.split(",")]
print(",".join(sorted(list(set(words)))))

Python program to displays that input back in upper and lower cases

a = input('Enter a string ')
print(a.upper())
print(a.lower())

Python program to count the occurrences of each word in a given sentence

a = input('Enter a string ')
dict = {}
words = a.split()
for word in words:
    keys = dict.keys()
    if word in keys:
        dict[word] += 1 # insert value in dict and add count
    else:
        dict[word] = 1
       
print(dict)

Python program to remove the characters which have odd index values of a given string

a = input('Enter a string ')
b=''
i=0
for i in range(len(a)):
    if(i%2==0):
        b = b+a[i]
print(b)

Python program to change a given string to a new string where the first and last chars have been exchanged

a = input('Enter a string ')
c=len(a)
d=a[1:(c-1)]
print(d)
d = a[c-1]+d+a[0]
print(d)

Python program to remove the nth index character from a nonempty string

a = input('Enter a string ')
b=a
c=int(input('inter position to remove character : '))
b = a[:c-1]+a[c:]
print(b)

Write a Python function that takes a list of words and returns the length of the longest one

list1 = []
list2 = []
b = int(input('enter no of words'))
for i in range(b):
    c = input('enter word : ')
    list1.append(c)
    list2.append(len(c))
c = list2.index(max(list2))
print('longest word is ',list1[c],' length is ',list2[c])

Write a Python program to find the first appearance of the substring 'not' and 'poor' from a given string, if 'not' follows the 'poor', replace the whole 'not'...'poor' substring with 'good'. Return the resulting string

a = input('Enter a string ')
b=a
c=d=0
c = a.find('not')
d = a.find('poor')
print(c,d)
if(c>=0 and d>=0):
    b = b.replace(b[c:d+4],'good')
print(b)

Write a Python program to add 'ing' at the end of a given string (length should be at least 3). If the given string already ends with 'ing' then add 'ly' instead. If the string length of the given string is less than 3, leave it unchanged

a = input('Enter a string ')
b = a[-3:]
if(a[-3:] == 'ing'):
    b = a +'ly'
elif(len(b) <3):
    print(b)
else:
    b =a+'ing'
print(b)

Python program where all occurrences of different char have been changed to '$' except first of that character

a = input('Enter a string ')
b = a
nc = ''
for n in a:
    c=0
    for m in b:
        if(m is n and c>0):
            nc = nc + '$'
        elif(m is n):
            c=c+1
            nc = nc + m
        else:
            nc = nc + m
    b = nc
    nc = ''
print(b)

Python program to get a string made of the first 2 and the last 2 chars from a given a string

a = input('Enter a string ': )
if len(a) < 2:
    print('')
print(a[0:2] + a[-2:])

Python program to count the number of characters (character frequency) in a string

a = input('Enter a string : ')
dict = {}
for n in a:
    keys = dict.keys()
    if n in keys:
        dict[n] += 1 # insert value in dict and add count
    else:
        dict[n] = 1
     
print(dict)

Python program to calculate the length of a string

a = input('Enter a string ')
b = len(a)
print('length of the string is ',b)

Python program to create a string from two given strings concatenating uncommon characters of the said strings

PROGRAM :  a = input('Enter a string : ') b = input('Enter a string : ') c='' for i in a:     if(i not in b):...