Python ProgrammingPython Programming

This program demonstrates use of various 15 string methods.

Python provides a wide variety of tools and programming techniques that you can use to examine and manipulate strings.This program displays various messages about the string, depending on the return value of the methods. Strings are immutable, meaning they cannot be modified, they do have a number of methods that return modified versions of themselves.
##
# Python's program to determine string entered by user is a palindrome.


def main():
    print('\n########################################################\n')

    user_string = "China"
    if user_string.isalnum():
        print(user_string, ': The String is alphanumeric.\n')

    user_string = "1974"
    if user_string.isdigit():
        print(user_string, ': The String contains only digits.\n')

    user_string = "Australia"
    if user_string.isalpha():
        print(user_string, ': String contains only alphabetic characters.\n')

    user_string = "   "
    if user_string.isspace():
        print(user_string, ': String contains only whitespace characters.\n')

    user_string = "JAPAN"
    if user_string.isupper():
        print(user_string, ': All letters in the string are uppercase.\n')

    user_string = "Australia 1974"
    if user_string.islower():
        print(user_string, ': All letters in the string are lowercase.\n')

    user_string = "India 1974"
    toLower = user_string.lower()
    print(user_string, ': Convert String in lowercase:', toLower, '\n')

    toUpper = user_string.upper()
    print(user_string, ': Convert String in uppercase:', toUpper, '\n')

    # lstrip() Returns a copy of the string with all leading whitespace
    # characters removed, that appear at the beginning of the string.
    user_string = "   India 1974"
    print('\nLeft Strip:', user_string.lstrip())

    # lstrip(char) Returns a copy of the string with all instances of
    # char that appear at the beginning of the string removed.
    user_string = "America 1974"
    print('\nLeft Strip with Character A:', user_string.lstrip('A'))

    # rstrip() Returns a copy of the string with all leading whitespace
    # characters removed, that appear at the end of the string.
    user_string = "America 1974     "
    print('\nRight Strip:', user_string.rstrip('A'))

    # rstrip(char) Returns a copy of the string with all instances of
    # char that appear at the end of the string.
    user_string = "America"
    print('\nRight Strip with Character a:', user_string.rstrip('a'))

    # strip() returns a copy of the string with all leading
    # and trailing whitespace characters removed.
    user_string = "   America 1974    "
    print('\nStrip from both side :', user_string.strip())

    # strip(char) Returns a copy of the string with all instances of
    # char that appear at the beginning and the end of the string removed.
    user_string = "America Russia Japan China"
    print('\nStrip with Character A:', user_string.strip('A'))
    print('Strip with Character a:', user_string.strip('a'))

    # endswith(substring) The substring argument is a string.
    # The method returns true if the string ends with substring.
    print('\nendswith check whether a string ends with a specified substring')
    user_string = "Columbia"
    print(user_string.endswith('bia'))
    print(user_string.endswith('mia'))

    # startswith(substring) The substring argument is a string.
    # The method returns true if the string starts with substring.
    print('\nstartswith check if a string starts with a specified substring')
    user_string = "Columbia"
    print(user_string.startswith('Colum'))
    print(user_string.startswith('Rolum'))

    # The substring argument is a string. The method returns the lowest
    # index in the string where substring is found.
    print('\n################## Find Method ##################')
    user_string = "Columbia"
    print(user_string.find('lumbia'))
    print(user_string.find('colbia'))
    print(user_string.find('bia'))
    print(user_string.find('col'))
    print(user_string.find('Col'))

    # replace(old, new) The old and new arguments are both strings. The method
    # returns a copy of the string with all instances of old replaced by new.
    print('\n################## Replace Method ##################')
    user_string = "Hello Columbia"
    old = "Columbia"
    new = "Russia"
    print(user_string.replace(old, new))

main()

Sample output of above program.
C:\Python\programs>pep8 --first program18.py

C:\Python\programs>python program18.py

########################################################

China : The String is alphanumeric.

1974 : The String contains only digits.

Australia : The String contains only alphabetic characters.

: The String contains only whitespace characters.

JAPAN : All letters in the string are uppercase.

India 1974 : Convert String in lowercase: india 1974

India 1974 : Convert String in uppercase: INDIA 1974


Left Strip: India 1974

Left Strip with Character A: merica 1974

Right Strip: America 1974

Right Strip with Character a: Americ

Strip from both side : America 1974

Strip with Character A: merica Russia Japan China
Strip with Character a: America Russia Japan Chin

endswith check whether a string ends with a specified substring
True
False

startswith check if a string starts with a specified substring
True
False

################## Find Method ##################
2
-1
5
-1
0

################## Replace Method ##################
Hello Russia

C:\Python\programs>