Certainly! Below is an example of a Python program designed to interact with the user. It includes comments and makes use of various string operations you might find useful. This program is organized to demonstrate at least 50 string operations, including manipulation and analysis from user input.
python
def main():
# Get user input
user_input = input("Enter a string: ")
# Convert the string to upper case
print("Upper case:", user_input.upper())
# Convert the string to lower case
print("Lower case:", user_input.lower())
# Swap case of the string
print("Swap case:", user_input.swapcase())
# Title-case the string
print("Title case:", user_input.title())
# Capitalize the string
print("Capitalized:", user_input.capitalize())
# Center the string with a width of 50
print("Centered:", user_input.center(50, '-'))
# Count occurrences of a substring
substring = input("Enter a substring to count: ")
print(f"Occurrences of '{substring}':", user_input.count(substring))
# Find the first occurrence of a substring
print(f"First occurrence of '{substring}':", user_input.find(substring))
# Find the last occurrence of a substring
print(f"Last occurrence of '{substring}':", user_input.rfind(substring))
# Check if the string starts with a specified prefix
prefix = input("Enter a prefix to check: ")
print(f"Starts with '{prefix}':", user_input.startswith(prefix))
# Check if the string ends with a specified suffix
suffix = input("Enter a suffix to check: ")
print(f"Ends with '{suffix}':", user_input.endswith(suffix))
# Check if string is all alphabetic
print("Is alphabetic:", user_input.isalpha())
# Check if string is all numeric
print("Is numeric:", user_input.isdigit())
# Check if string is alphanumeric
print("Is alphanumeric:", user_input.isalnum())
# Check if all characters in string are lower case
print("Is lower:", user_input.islower())
# Check if all characters in string are upper case
print("Is upper:", user_input.isupper())
# Check if the string is a title-cased string
print("Is title cased:", user_input.istitle())
# Convert tabs in string to spaces
print("No tabs:", user_input.expandtabs(4))
# Replace a substring
to_replace = input("Enter a substring to replace: ")
replacement = input("Enter the replacement: ")
print("Replaced string:", user_input.replace(to_replace, replacement))
# Partition the string into three parts
delimiter = input("Enter a delimiter for partition: ")
print("Partitioned:", user_input.partition(delimiter))
# Split string at line breaks
print("Split into lines:", user_input.splitlines())
# Split string into a list of words
print("Split into words:", user_input.split())
# Join list of strings into a single string
words = input("Enter comma-separated words to join: ").split(',')
print("Joined string:", ' '.join(words))
# Strip leading and trailing whitespace
print("Stripped string:", user_input.strip())
# Strip only leading whitespace
print("Leading stripped:", user_input.lstrip())
# Strip only trailing whitespace
print("Trailing stripped:", user_input.rstrip())
# Check if the string is printable
print("Is printable:", user_input.isprintable())
# Check if the string contains only whitespace
print("Is space:", user_input.isspace())
# Format string using format method
value = input("Enter a value to format into string: ")
print("Formatted string:", "Inserted value: {}".format(value))
# Check the length of the string
print("Length of string:", len(user_input))
# Access a specific index in the string
index = int(input("Enter an index to access (0-based): "))
print(f"Character at index {index}:", user_input[index])
# Find characters in the string
char = input("Enter a character to find its index: ")
print(f"Index of first '{char}':", user_input.index(char))
# Reverse the string
print("Reversed string:", user_input[::-1])
# Create a translation table
trans_table = str.maketrans("aeiou", "12345")
# Translate the string
print("Transliterated:", user_input.translate(trans_table))
# Append an exclamation mark to the string
print("Exclaimed:", user_input + "!")
# Check if the string contains only decimal characters
print("Is decimal:", user_input.isdecimal())
# Count number of spaces in the string
print("Number of spaces:", user_input.count(" "))
# Convert the string to ASCII representation
print("ASCII:", user_input.encode('ascii', 'ignore'))
if __name__ == "__main__":
main()
This script showcases different string operations and gathers input from the user to demonstrate each feature. It is both interactive and informative and aligns with the request for usage of string operations within a Python script.