Using Python's String Slicing to Add/Replace Characters in Specific Positions
Intro
This article demonstrates how to replace characters in one variable for the characters in another using the classic game of hangman. It will show the game code, the main problem I encountered, and a solution to that problem.
The Game
word = "hello"perm_word = "hello"blank = "-" * len(word)dex = ""count = len(word) * 2print(f"This is a hangman game. Can you guess the word: {blank} in 10 tries?")while blank != perm_word and count != 0: guess = input("Guess a letter: ") if len(guess) > 1: print("Type a single letter.") continue for char in guess: if char in word: dex = word.find(char) blank = blank[:dex] + char + blank[dex+1:] word = word[:dex] + "-" + word[dex+1:] count -= 1 print(blank) print(f"Correct! You have {count} tries left.") else: count -= 1 print(f"Wrong. You have {count} tries left.")print("You win!")
THE PROBLEM
All of the code executed properly except for the lines that replace the dashes in blank
with the properly guessed characters from word
.
When played, the game printed a correct guess as a single character like this: e
. If the second guess was an h
the game would print eh
instead of he
.
THE SOLUTION
The key thing to remember for this problem is that strings in Python are immutable. To replace or add characters to a string, Python needs to create a copy of the string and replace characters in that copy.
The following three lines of code provide this function.
dex = word.find(char) blank = blank[:dex] + char + blank[dex+1:]word = word[:dex] + "-" + word[dex+1:]
The first line gets the index value for the guessed character.
Using this value, the second line slices the dashes in blank
by selecting all the index values before the guessed character with blank[:dex]
. It then concatenates this first slice with the guessed character and the ending slice. blank[dex+1:]
is the final slice. It selects all of the remaining index values after the guessed character.
Now if you guess e
the game prints: -e---
.
Problem solved? Not quite.
The word "hello" has two "l's". Everything works as expected for the first "l". The game prints: -el--
. However, when a second "l" is guessed, the game stops at the first "l" and continues to print: -el--
.
The third line of the code above solves this. It functions similarly to the previous line except it operates on word
instead of blank
. Essentially this makes the two variables exchange their values as characters are guessed.
If l
is guessed the values of blank
and word
will look like this:
blank = "--l--"
word = "he-lo"
Now when the second l
is guessed, word
only contains one l
. The game functions as it should. The values of blank
and word
will look like this:
blank = "--ll-"
word = "he--o"
ADDING A CHARACTER
To add a character in a specific position instead of replacing it, remove the +1
from dex
in the final slice. For example, if e
is guessed the following line of code:
word = word[:dex] + "-" + word[dex:]
will print:
"h-ello"
Thanks for reading!
Original Link: https://dev.to/josephmads/using-pythons-string-slicing-to-addreplace-characters-in-specific-positions-1ed5
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To