7. Implementing programs using Strings. (reverse, palindrome, character count, replacing characters)

 

7. Implementing programs using Strings. (reverse, palindrome, character count, replacing characters)

REVERSE A STRING

def reversed_string(text):

     if len(text) == 1:

       return text

     return reversed_string(text[1:]) + text[:1]

print(reversed_string("Python Programming!"))

 

PALINDROME

def isPalindrome(s):

    return s == s[::-1]

 

s = input("enter any string :")

ans = isPalindrome(s)

 if ans:

    print(s,"Yes it's a palindrome")

else:

    print(s,"No it's not a palindrome")

CHARACTER COUNT

def count_chars(txt):

            result = 0

            for char in txt:

                        result += 1     # same as result = result + 1

            return result

text=input("enter any string")

print("no.of characters in a string",count_chars(text))

REPLACING CHARACTERS

string = "python is a programming language is powerful"

print(string.replace("p", "P",1))

print(string.replace("p", "P"))

Comments

Popular posts from this blog

10. Implementing real-time/technical applications using Exception handling. (divide by zero error, voter’s age validity, student mark range validation)

11. Exploring Pygame tool.