Wednesday, 5 January 2022

Python String Practice

Below are the set of  16 questions which will help to give the basic knowledge of all type of programming of Strings in Python. You can visit website pynative.com or https://www.geeksforgeeks.org, https://www.w3resource.com//.

Steps to run below programs:

  1. Install Anaconda in your PC/ Laptop.
  2. Open Spyder.
  3. Copy and paste below programs.
  4. Press Run button. You can see the result in Console section at right side.



#1. Program to return the string without any whitespace 

# Answer: Akansha Aggarwal

# Solution:

txt = " Akansha Aggarwal "

x = txt.strip()

print(x)


# 2. Program to replace the character A with a M.

# Answer: akansha aggarwal

# Solution:

txt = "akansha aggarwal"

txt = txt.replace("A", "M")

print(txt)


#3. Program to get a string from a given string where all occurrences of its first char have been changed to '$', except the first char itself.

# Answer: ak$nsh$

# Solution:

def changed_char(str1):

  char = str1[0]

  str1 = str1.replace(char, '$')

  str1 = char + str1[1:]

  return str1

print(changed_char('akansha'))


#4. Program to get a single string from two given strings, separated by a space and swap the first two characters of each string.

# Answer: rsz xyt

# Solution:

def chars_mix_up(a, b):

  new_a = b[:2] + a[2:]

  new_b = a[:2] + b[2:]

  return new_a + ' ' + new_b

print(chars_mix_up('xyz', 'rst'))


#5. Program to remove the nth index character from a nonempty string.

# Answer: kansha, Akasha, Akansa

# Solution:

def remove_char(str, n):

      first_part = str[:n] 

      last_part = str[n+1:]

      return first_part + last_part

print(remove_char('Akansha', 0))

print(remove_char('Akansha', 3))

print(remove_char('Akansha', 5))


#6. Program to remove the characters which have odd index values of a given string.

# Answer: ace

# Solution:

def odd_string(str):

  result = "" 

  for i in range(len(str)):

    if i % 2 == 0:

      result = result + str[i]

  return result

print(odd_string('abcdef'))


#7. Program to find whether this string is a palindrome or not. (A palindrome is a string that reads the same forwards and backwards.)

# Answer: 

# Solution:

word=input("Please enter a word")

word=str(word)

reverse=word[::-1]

print(reverse)

if word == reverse:

    print("This word is a palindrome")

else:

    print("This word is not a palindrome")

    

#8.  program to find first, last and middle characters of given string and make new string using it

# Answer: Ana

# Solution:

str1="Akansha"

first = str1[0]

l = len(str1)

mi = int(l/2)

middle = str1[mi]

last = str1[-1]

res= first +middle+last

print(res)


#9.  program to find middle three characters and make new string using it

# Answer: sha, yPo

# Solution:

def stringmiddlecharacters (str):

    mi = int(len(str)/2)

    result =str[mi-1:mi+2]

    print (result)

stringmiddlecharacters("AkanshaAgg")

stringmiddlecharacters("HarryPotter")


# 10. program to append new string in the middle of first string.

# Answer: AkaHarrynsha

# Solution:

str1= "Akansha"

str2= "Harry"

mi = int(len(str1)/2)

result = str1[0:mi] + str2 + str1[mi:]

print (result)


#11. program to find first, middle and last characters of each input string and make new string using it

# Answer: AHnray

# Solution:

s1="Akansha"

s2="Harry"

mi1 = int(len(s1)/2)

mi2 = int(len(s2)/2)

result = s1[0] + s2[0] + s1[mi1] + s2[mi2] + s1[-1] + s2[-1]

print (result)


# 12. program to arrange charcaters in such a way that lower case characters will come first and upper case in last

# Answer: knhAASA

# Solution:

str1= "AkAnShA"

lower=[]

upper=[]

for char in str1:

    if char.islower():

        lower.append(char)

    else:

        upper.append(char)

jointchar = ''.join(lower + upper)

print (jointchar)


# 13. program to count letters, digits and symbols from given string

# Answer: 8 3 4

# Solution:

def find_digits_chars_symbols(sample_str):

    char_count =0

    digit_count=0

    symbol_count=0

    for char in sample_str:

        if char.isalpha():

            char_count = char_count + 1

        elif char.isdigit():

            digit_count = digit_count + 1

        else:

            symbol_count = symbol_count + 1

    print (char_count, digit_count, symbol_count)  

find_digits_chars_symbols("A@#ka26ns^&h2ab")


# 14. program to check if two strings are balanced like if all the characters of one string is present in another string

# Answer: True, True

# Solution:  

def string_balance_test (s1,s2):

    flag = True

    for char in s1:

        if char in s2:

            continue

        else:

            flag = False

    return flag

flag = string_balance_test("ns", "Akansha")

print(flag)

flag = string_balance_test("nsh", "Akansha")

print(flag)


# 15. program to reverse the string

# Answer: aHsnakA

# Solution: 

str1 = "AkansHa"

str2 = str1[::-1]

print(str2)


# 16. program to remove all the characters from the string except integers

# Answer: 332022

# Solution:

str1= ' I am going to be 33 years old in 2022'

changed_Str = "".join([i for i in str1 if i.isdigit()])

print (changed_Str)


No comments:

Post a Comment