Write a choice based program to demonstrate the using of different string functions and methods. Take
a string as input from user and display the available choices, and call the respective functions and
methods. answer this
Answers
print("This program demonstrates the various string functions/methods.")
print()
while True:
print("Functions/Methods: ")
print()
print("1. Concatenation")
print("2. Repetition")
print("3. len()")
print("4. capitalize()")
print("5. split()")
print("6. replace()")
print("7. isalpha()")
print("8. isdigit()")
print("9. lower()")
print("10. islower()")
print("11. upper()")
print("12. isupper()")
print("13. isspace()")
print("14. join()")
print("15. swapcase()")
print("16. partition")
print("17. Exit")
print()
c = int(input("Enter the choice that you'd like to know more about: "))
print()
if c == 1:
print("Concatenation")
print()
print("Concatenation is simply adding two or more strings together using the '+' operator.")
print()
print("Let's test it.")
print()
c1 = input("Enter a string: ")
c2 = input("Enter another string: ")
print("Let's see what happens when we place the '+' operator between both strings.")
print()
print(c1 + c2)
print()
print("As it can be seen above, the two strings have been joined together.")
print("When you add two strings, there will be no space between them.")
print()
elif c == 2:
print("Repetition")
print()
print("Repetition is simply repeating a string using the '*' operator by a number.")
print()
print("Let's test it.")
print()
r1 = input("Enter a string: ")
r2 = int(input("Enter the number of times you want the string to be repeated: "))
print("Let's see what happens when we place the '*' operator between both strings.")
print()
print(r1*r2)
print()
print("As it can be seen above, the string has been repeated", r2, "times.")
print("When you multiply a string by a number, there will be no space in the result.")
print()
elif c == 3:
print("len()")
print()
print("len() is a method used to calculate the length/number of characters of a string.")
print()
print("To use len(), we simply mention the variable to which the string is stored within the brackets.")
print("Like this: len( <string_variable> )")
print()
print("Let's test it.")
print()
l1 = input("Enter a string: ")
print()
print(len(l1), "is the length of the string after using the len() function.")
print()
elif c == 4:
print("capitalize()")
print()
print("This method returns the string with the first character capitalized.")
print("The syntax of this function is as follows: ")
print("string_name.capitalize()")
print()
print("Let's test it.")
print()
c1 = input("Enter a string: ")
print()
print(c1.capitalize(), "is the output after using capitalize().")
print()
elif c == 5:
print("split()")
print()
print("This method breaks the string at a specified separator and returns the result in the form of a list.")
print("The syntax of this function is as follows: ")
print("string_name.split(separator)")
print()
print("Let's test it.")
print()
s1 = input("Enter a string: ")
s2 = input("Enter a character where the string needs to separate: ")
print()
print(s1.split(s2), "is the result when split() is used.")
print()
elif c == 6:
print("replace() is a function that replaces the occurences of the old string with the new specified string.")
print("The syntax is as follows: ")
print("string_name.replace(old string, new string)")
print()
print("Let's test it.")
print()
r1 = input("Enter a string [a sentence is mostly preferred to observe the difference: ")
r2 = input("Enter the string/character to be replaced: ")
r3 = input("Enter the new string: ")
print()
print(r1.replace(r2, r3), "is the result.")
print()
elif c == 7:
print("isalpha() is a method that checks if a string has only letters and no other special characters: ")
print("This means that it results to 'True' only if the string's characters contain letters from A - Z/a - z.")
print("The syntax is as follows: ")
print("string_name.isalpha()")
print("'True' = the string has only characters from A - Z/a - z.")
print("'False' = the string has other special characters.")
print()
print("Let's test it.")
print()
i1 = input("Enter a string: ")
print()
print(i1.isalpha(), "is the result.")
print()
elif c == 8:
print("isdigit() is a method that checks if a string has only digit characters.")
print("This means that the string must have only characters from 0 - 9 to result to 'True'.")
print("The syntax is as follows: ")
print("string_name.isdigit()")
print("'True' = the string has only characters 0 - 9.")
print("'False' = the string has other special characters.")
print()
print("Let's test it.")
print()
i1 = input("Enter a string: ")
print()
print(i1.isdigit(), "is the result.")
print()
Answer- Continuity of @Equestriadash's answer...
elif c == 9:
print("lower() is a method that converts all the letters to lowercase.")
print("All the uppercase letters [if any] will be converted to lowercase.")
print("The syntax is as follows: ")
print("string_name.lower()")
print()
print("Let's test it.")
print()
l1 = input("Enter a string: ")
print()
print(l1.lower(), "is the result.")
print()
elif c == 10:
print("islower() is a method that checks if the given string is in lowercase or not.")
print("This means that it will result to 'True' only if all the characters are in lowercase.")
print("The syntax is as follows: ")
print("string_name.islower()")
print("'True' = the string is in lowercase.")
print("'False' = the string is not in lowercase/not fully lowercase.")
print()
print("Let's test it.")
print()
i1 = input("Enter a string: ")
print()
print(i1.islower(), "is the result.")
print()
elif c == 11:
print("upper() is a method that converts all the letters into uppercase.")
print("All the lowercase letters [if any] will be converted to uppercase.")
print("The syntax is as follows: ")
print("string_name.upper()")
print()
print("Let's test it.")
print()
u1 = input("Enter a string: ")
print()
print(u1.upper(), "is the result.")
print()
elif c == 12:
print("isupper() is a method that checks if all the characters are in uppercase.")
print("This means that it will result to 'True' only if all the characters are in uppercase.")
print("The syntax is as follows: ")
print("string_name.isupper()")
print("'True' = the string is in uppercase.")
print("'False' = the string is not in uppercase/not fully uppercase.")
print()
print("Let's test it.")
print()
i1 = input("Enter a string: ")
print()
print(i1.isupper(), "is the result.")
print()
elif c == 13:
print("isspace() is a method that checks if there are space characters in the string.")
print("This means that it will result to 'True' only if there are space characters, or say you give an empty string.")
print("The syntax is as follows: ")
print("string_name.islower()")
print("'True' = the string contains only space characters.")
print("'False' = the string contains special characters.")
print()
print("Let's test it.")
print()
i1 = input("Enter a string: ")
print()
print(i1.isspace(), "is the result.")
print()
elif c == 14:
print("join() is a method that returns the string with the specified character joining the characters in the string.")
print("The syntax is as follows: ")
print("joining_character.join(string name)")
print()
print("Let's test it.")
print()
j1 = input("Enter a string: ")
j2 = input("Enter the joining character: ")
print()
print(j2.join(j1), "is the result.")
print()
elif c == 15:
print("swapcase() is a method that swaps the case of the letters in the string.")
print("This means that any uppercase characters in the string will become lowercase and any lower case characters in the string will become uppercase.")
print("The syntax is as follows: ")
print("string_name.swapcase()")
print()
print("Let's test it.")
print()
s1 = input("Enter a string: ")
print()
print(s1.swapcase(), "is the result.")
print()
elif c == 16:
print("partition() is a method that splits the given string at the specified operator.")
print("Unlike split() that splits the string at every occurrence of the specified separator, partition() only returns the string parting the string at the first occurrence of the specified separator and even returns the result in the form of a tuple including the separator.")
print("The syntax is as follows: ")
print("string_name.partition")
print()
print("Let's test it.")
print()
p1 = input("Enter a string: ")
p2 = input("Enter the separator: ")
print()
print(p1.partition(p2), "is the result.")
print()
elif c == 17:
break