Categories:Viewed: 4 - Published at: a few seconds ago

Write a python function that takes input a string and removes duplicates from the same

foo = input("Enter String : ")
print("Duplicates Removed","".join(set(foo)))

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.

def add_string(str1):
  length = len(str1)

  if length > 2:
    if str1[-3:] == 'ing':
      str1 += 'ly'
    else:
      str1 += 'ing'

  return str1

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.

def not_poor(str1):
  snot = str1.find('not')
  spoor = str1.find('poor')


  if spoor > snot and snot>0 and spoor>0:
    str1 = str1.replace(str1[snot:(spoor+4)], 'good')
    return str1
  else:
    return str1

### Write a Python program to count the occurrences of each word in a given sentence.
def word_count(str):
    counts = dict()
    words = str.split()

    for word in words:
        if word in counts:
            counts[word] += 1
        else:
            counts[word] = 1

    return counts

Write a Python function to create the HTML string with tags around the word(s).

def add_tags(tag, word):
    return "<%s>%s</%s>" % (tag, word, tag)

Write a Python program to count the number of even and odd numbers from a series of numbers.

numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9) # Declaring the tuple
count_odd = 0
count_even = 0
for x in numbers:
        if not x % 2:
             count_even+=1
        else:
             count_odd+=1

#Write a Python program that prints each item and its corresponding type from the following list.

datalist = [1452, 11.23, 1+2j, True, 'w3resource', (0, -1), [5, 12],
{"class":'V', "section":'A'}]
for item in datalist:
   print ("Type of ",item, " is ", type(item))

Write a Python program to sort (ascending) a dictionary by value.

d = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}

print({k :v for k,v in sorted(d.items(),key = lambda x : x[1])})

Write a Python program to sort (Descending) a dictionary by value.

d = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}

print({k :v for k,v in sorted(d.items(),key = lambda x : x[1],reverse = True)})