Python Programming Questions - Operation on STRING[SET-1]
1.Python program to check if a string is palindrome or not
inp = input(" Enter The String To Be Checked : ")
low = inp.lower()
if(low == low[::-1]):
print(inp,"Is Palindrome . ")
else:
print(inp,"Is Not Palindrome . ")
length = int(input(" Enter The Number of Elements Present In List :"))
myList = []
palindrome_list = []
no_palindromeList = []
for i in range(length):
myList.append(input())
for elements in myList:
lowerCase = elements.lower()
if(lowerCase == lowerCase[::-1]):
palindrome_list.append(elements)
else:
no_palindromeList.append(elements)
print(palindrome_list)
print(no_palindromeList)
Enter The String To Be Checked : malayalam
malayalam Is Palindrome .
Enter The Number of Elements Present In List :5
malayalam
radar
books
institute
dad
['malayalam', 'radar', 'dad']
['books', 'institute']
2.Python Program To Reverse words in a given String
inp = input(" Enter The Word You Want To Reverse : ")
rev = inp[::-1]
print(rev)
length = int(input(" Enter The Number Of Elemts Present In List : "))
myList = []
reverseList = []
for i in range(length):
myList.append(input())
for j in myList:
reverse = j[::-1]
reverseList.append(reverse)
print(myList)
print(reverseList)
Enter The Word You Want To Reverse : mobile
elibom
Enter The Number Of Elemts Present In List : 5
python
java
c++
csharp
html
['python', 'java', 'c++', 'csharp', 'html']
['nohtyp', 'avaj', '++c', 'prahsc', 'lmth']
3.Python program to remove i’th character from string
inp = input(" Enter The String : ")
ithValue = int(input(" Enter The Position For Which Element IS To Be Removed : "))
ind = ithValue-1
new_string = ""
new_string = new_string + inp.replace(inp[ind],'',1)
print("",new_string)
count = int(input(" Enter The Number of Elements Present In List : "))
myList = []
for i in range(count):
myList.append(input())
iValue = int(input(" Enter The Position Of Element To Be Removed : "))
indx = iValue - 1
new = ""
newList = []
for j in range(len(myList)):
obj = myList[j]
new = obj.replace(obj[indx],'',1)
newList.append(new)
print("",myList)
print("",newList)
Enter The String : MOBILE
Enter The Position For Which Element IS To Be Removed : 2
MBILE
Enter The Number of Elements Present In List : 5
MOBILE
LAPTOP
PYTHON
JAVA
HTML5
Enter The Position Of Element To Be Removed : 2
['MOBILE', 'LAPTOP', 'PYTHON', 'JAVA', 'HTML5']
['MBILE', 'LPTOP', 'PTHON', 'JVA', 'HML5']
4. Python Program To Check if a Sub string is Present in a Given String
stringInput = input(" Enter The String : ")
substringInput = input(" Enter The Substring : ")
if(stringInput.find(substringInput) == -1):
print("",substringInput," Not Found ")
else:
print(""substringInput, " Found ")
count = int(input(" Number Of Element Prsent In List : "))
myList = []
for i in range(count):
myList.append(input())
print(" Enter The Substring : ")
for j in range(count):
substringInp = input()
if(myList[j].find(substringInp) == -1):
print("",substringInp,"Not Found In String ",myList[j])
else:
print("",substringInp,"Found In String ",myList[j])
Enter The String : LAPTOP
Enter The Substring : op
op Not Found
Number Of Element Prsent In List : 5
("WEB","DEVELOPMENT","HTML5","CSS3")
("DEVELOPMENT","PYTHON","JAVASCRIPT","JAVA")
PYTHON IS USED IN MACHINE LEARNING
PYCHARM IS IDE USED FOR PYTHON
LETS DO IT
Enter The Substring :
HTML5
HTML5 Found In String ("WEB","DEVELOPMENT","HTML5","CSS3")
C++
C++ Not Found In String ("DEVELOPMENT","PYTHON","JAVASCRIPT","JAVA")
In
In Not Found In String PYTHON IS USED IN MACHINE LEARNING
IDE
IDE Found In String PYCHARM IS IDE USED FOR PYTHON
DO
DO Found In String LETS DO IT
5. Python Program To Find Length Of String
stringInput = input(" Enter Input String : ")
#METHOD1
length1 = len(stringInput)
#METHOD2
length2 = 0
for i in stringInput:
length2 = length2 + 1
#METHOD3
length3 = 0
while stringInput[length3:]:
length3 = length3 + 1
print("By Method 1 ",length1)
print("By Method 2 ",length2)
print("By Method 3 ",length3)
Enter Input String : PYTHON IS FUN.
By Method 1 14
By Method 2 14
By Method 3 14
Comments
Post a Comment