Python Programs on Strings
- Write a program that asks the user to enter a string and perform the following:
- The total number of characters in the string.
- Repeat the string 10 times.
- The first character of the string.
- The first three characters of the string.
- The last three characters of the string.
- The string in backwards.
- The seventh character of the string if exist otherwise display a message “Not exist”.
- The string with its first and last characters removed.
- The string into capital case.
- The string with every a replaced with an e.
- The string with every letter replaced by a space.
Logic:
- The input() function is used to read user input from the console (standard input, typically the keyboard)
- The len() function returns the number of characters in a string, including spaces and special characters.
- Python's string multiplication operator (*) repeats a string a specified number of times.
- String indexing allows accessing a character at a specific position using [index]. The first character is at index 0.
- String slicing ([start:end]) extracts a substring. The slice [:3] gets characters from the start (index 0) up to, but not including, index 3.
- Negative indexing in Python counts from the end of the string. The slice [-3:] starts at the third character from the end and goes to the end.
- Python's slice notation [start:end:step] with a step of -1 reverses the string. The slice [::-1] means "start from the end, go to the beginning, with a step of -1".
- The seventh character is at index 6 (since indexing starts at 0). The program checks if the string has at least 7 characters before accessing the index.
- The slice [1:-1] starts at index 1 (second character) and goes up to, but not including, the last character (index -1).
- The upper() method converts all lowercase letters in a string to uppercase, leaving non-letter characters unchanged.
- The replace (old, new) method replaces all occurrences of the substring old with new.
- Iterate through each character in the string. If the character is a letter (checked using isalpha()), replace it with a space (' '); otherwise, keep the character unchanged. Join the resulting characters into a single string.
Program:
########## Strings #################
# Program to perform various string operations
user_string = input("Enter a string: ")
# (b) Total number of characters in the string
total_chars = len(user_string)
print("Total number of characters:", total_chars)
# (c) Repeat the string 10 times
repeated_string = user_string * 10
print("String repeated 10 times:", repeated_string)
# (d) First character of the string
first_char = user_string[0]
print("First character:", first_char)
# (e) First three characters of the string
if len(user_string) >= 3:
first_three = user_string[:3]
else:
first_three = user_string
print(" First three characters:", first_three)
# (f) Last three characters of the string
if len(user_string) >= 3:
last_three = user_string[-3:]
else:
last_three = user_string
print(" Last three characters:", last_three)
# (g) String in backwards
backwards = user_string[::-1]
print("String backwards:",backwards)
# (h) Seventh character of the string
if len(user_string) >= 7:
seventh_char = user_string[6]
print("Seventh character:", seventh_char)
else:
print("Not exist")
# (i) String with first and last characters removed
middle_string = user_string[1:-1] if len(user_string) > 2 else ""
print("String without first and last characters:", middle_string)
# (j) String in capital case
capital_string = user_string.upper()
print("String in capital case:", capital_string)
# (k) String with every 'a' replaced with 'e'
replace_a_with_e = user_string.replace('a', 'e')
print("String with 'a' replaced by 'e':", replace_a_with_e)
# (l) String with every letter replaced by a space
space_string = ''.join(' ' if c.isalpha() else c for c in user_string)
print("String with letters replaced by spaces:", space_string)
Output: