Python Programs on Strings

  1. Write a program that asks the user to enter a string and perform the following:
  2. The total number of characters in the string.
  3. Repeat the string 10 times.
  4. The first character of the string.
  5. The first three characters of the string.
  6. The last three characters of the string.
  7. The string in backwards.
  8. The seventh character of the string if exist otherwise display a message “Not exist”.
  9. The string with its first and last characters removed.
  10. The string into capital case.
  11. The string with every a replaced with an e.
  12. The string with every letter replaced by a space.

Logic:

  1. The input() function is used to read user input from the console (standard input, typically the keyboard)
  2. The len() function returns the number of characters in a string, including spaces and special characters.
  3. Python's string multiplication operator (*) repeats a string a specified number of times.
  4. String indexing allows accessing a character at a specific position using [index]. The first character is at index 0.
  5. String slicing ([start:end]) extracts a substring. The slice [:3] gets characters from the start (index 0) up to, but not including, index 3.
  6. 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.
  7. 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".
  8. 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.
  9. The slice [1:-1] starts at index 1 (second character) and goes up to, but not including, the last character (index -1).
  10. The upper() method converts all lowercase letters in a string to uppercase, leaving non-letter characters unchanged.
  11. The replace (old, new) method replaces all occurrences of the substring old with new.
  12. 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:

Program on Strings in Python