You are on page 1of 3

IMPLEMENTING THE PROGRAM USING STRING

Method 1: Reverse a Number using a while loop

def my_function(x):

return x[::-1]

mytxt = my_function("I wonder how this text looks like backwards")

print(mytxt)

OUTPUT

sdrawkcab ekil skool txet siht woh rednow I

Method 2:Using String slicing

Str1= input(“enter a string:”)

print(str(Str1)[::-1])

OUTPUT

enter a string:hello

olleh

PALINDROME

def isPalindrome(s):

return s == s[::-1]

s = input("enter the string")

ans = isPalindrome(s)
if ans:

print("Yes")

else:

print("No")

COUNT OF STRING

test_str = "HEELLO WORLD"

count = 0

for i in test_str:

if i == 'E':

count = count + 1

print ("Count of string : "+ str(count))

OUTPUT

Count of string : 2

REPLACING CHARACTERS

METHOD 1:

txt = "I like bananas"

x = txt.replace("bananas", "apples")
print(x)

OUTPUT:

I like apples
METHOD 2:

string = "PROBLEM SOLVING AND PYTHON PROGRAMMING"

print(string.replace("E", "a"))

print(string.replace("G", "H", 1))

OUTPUT

PROBLaM SOLVING AND PYTHON PROGRAMMING

PROBLEM SOLVINH AND PYTHON PROGRAMMING

You might also like