Skip to content

Instantly share code, notes, and snippets.

@sshehrozali
Created November 9, 2020 18:54
Show Gist options
  • Save sshehrozali/5a5baf58dfc3b05166d7df4b917498aa to your computer and use it in GitHub Desktop.
Save sshehrozali/5a5baf58dfc3b05166d7df4b917498aa to your computer and use it in GitHub Desktop.
Python code to insert an alphabet in a string
# Store any word
string = input("Enter any word: ")
# Ask where to insert before
index_Alpha = input("Where do you want to insert?\nEnter Alphabet: ")
# Int to store string index no
index_No = 0
# Iterate until char matches, get index no
for i in range(len(string)):
# If matches
if string[i] == index_Alpha:
# Ask for alpha to insert
new_Alpha = input("Alphabet to insert: ")
# Get index no
index_No = i
break
# List to store new string
new_string = []
# Copy each char from old string to new string
for j in range(len(string)):
new_string.insert(j, string[j])
# Insert new char at index no declared above
new_string.insert(index_No, new_Alpha)
# Print each char on screen
for k in range(len(new_string)):
print(new_string[k], end="")
# Print new-line, exit the program
print("")
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment