Skip to content

Latest commit

 

History

History
163 lines (115 loc) · 1.96 KB

Python_Interview_Questions_and_Answers_Strings.md

File metadata and controls

163 lines (115 loc) · 1.96 KB

Python Interview Questions and Answers - Strings

1. Python program to remove character from string

Hint

Input - Python

Input Character - o

Output - Pythn

str = "Python"
ch = "o"
print(str.replace(ch," ")) 

>> Pyth n

2. Python Program to count occurrence of characters in string

Hint

Input - Python

Input Character - o

Output - 1

string = "Python"
char = "y"
count = 0
for i in range(len(string)):
    if(string[i] == char):
        count = count + 1
print(count)

>> 1

3. Python program in to check string is anagrams or not

Hint

Input - Python

Input Character - onypth

Output - Anagrams

str1 = "python"
str2 = "yonthp"
if (sorted(str1) == sorted(str2)):
    print("Anagram")
else:
    print("Not an anagram")
    
> Anagram

4. Python program to check string is palindrome or not

Hint

Input - madam

Output - Palindrome

string = "madam"
if(string == string[:: - 1]):
   print("Palindrome")
else:
   print("Not a Palindrome") 

> Palindrome

5. Python code to check given character is digit or not

Hint

Input - a

Output - Not a Digit

ch = 'a'
if ch >= '0' and ch <= '9': 
    	print("Digit")
else: 
    print("Not a Digit")
    
> Not a Digit

6. Program to replace the string space with any given character

Hint

Input - m m

Input charcter - a

Output - mam

string = "m d m"
result = '' 
ch = "a"
for i in string:  
        if i == ' ':  
            i = ch   
        result += i  
print(result)
    
> madam