Reputation: 23
I am trying to build a program that will pick a random word from a list, and display that word with alternate letters being replaced by hyphens. for example, the word "monkey" would be displayed as "-o-k-y". I can get a random word selected easy enough using random.choice, I know I should use a FOR loop, but I just can't seem to get my head around it.
Upvotes: 1
Views: 1959
Reputation: 55489
There's a way to do this task without using explicit looping or Regular Expressions, just using built-in str
methods. It's a little bit tricky, so I'll explain how it works in stages.
Firstly, we can get every 2nd item in a sequence using extended slice notation. This is generally used on lists or tuples, but it also works on strings. Eg,
>>> 'abcdef'[::2]
'ace'
>>> 'abcdefg'[::2]
'aceg'
>>> 'abcdefg'[1::2]
'bdf'
We can join together a sequence of characters using the str.join
method. The string we call .join
on is used as the separator between the items in the arg to .join
. This is mostly used to join together a sequence of strings, but it also works on single strings. Eg,
>>> '-'.join('abcdef')
'a-b-c-d-e-f'
>>> '-*-'.join('abcdef')
'a-*-b-*-c-*-d-*-e-*-f'
Another method that's useful for this task is str.ljust
. This method allows us to pad a string up to a given length, using a character of our choice (the default padding char is an ASCII space). The original characters go to the left side of the output string, the padding is added to the right. If the string is longer than the requested length, it gets returned unmodified.
>>> 'abcd'.ljust(6, '-')
'abcd--'
>>> 'abcdef'.ljust(4, '-')
'abcdef'
We can perform the string transformation you desire using these tools.
>>> 'monkey'[1::2]
'oky'
>>> '-'.join('monkey'[1::2])
'o-k-y'
>>> '-' + '-'.join('monkey'[1::2])
'-o-k-y'
So far, so good. But it doesn't quite work when the length of the word is odd:
>>> '-' + '-'.join('gorilla'[1::2])
'-o-i-l'
That should have a final dash. But we can use .ljust
to fix that:
>>> word = 'gorilla'
>>> s = '-' + '-'.join(word[1::2])
>>> s.ljust(len(word), '-')
'-o-i-l-'
Let's test it out on a few different words.
words = 'Here are some short test words monkey gorilla'.split()
for word in words:
dashed = ('-' + '-'.join(word[1::2])).ljust(len(word), '-')
print(word, dashed)
output
Here -e-e
are -r-
some -o-e
short -h-r-
test -e-t
words -o-d-
monkey -o-k-y
gorilla -o-i-l-
Jon Clements pointed out that there's there's an undocumented feature of the str.replace
method which we can use here. If you pass it an empty string as its first arg it will insert the replacement string at every position of the old string. Eg:
>>> 'abcdef'.replace('', '-')
'-a-b-c-d-e-f-'
We can use that undocumented feature to perform your task like this:
words = 'Here are some short test words monkey gorilla'.split()
for word in words:
dashed = word[1::2].replace('', '-')[:len(word)]
print(word, dashed)
The output is identical to the previous version, but Jon's code is almost twice as fast as my version. Thanks, Jon!
I tested this code on Python 2.5, 2.6, 3.1, and 3.6, and it works perfectly, but bear in mind that it's generally not a good idea to use undocumented features because they can be changed without notice.
Upvotes: 3
Reputation: 294
You could use a regular expression:
import re
word = re.sub(r'.(.)', r'-\1', "monkey")
Upvotes: 2
Reputation: 18950
Replacing the character at even indices with a generator expression:
>>> s
'Hello, world!'
>>> ''.join(c if i%2 else '-' for i, c in enumerate(s))
'-e-l-,-w-r-d-'
If you want the hyphens pattern to be random you can use random.sample()
with range(len(s))
as the population:
>>> n = 4
>>> mask = set(random.sample(range(len(s)), n))
{8, 1, 4, 0}
>>> ''.join('-' if i in mask else c for i, c in enumerate(s))
'--ll-, w-rld!'
Upvotes: 1
Reputation: 4814
Try this:
import random
word = random.choice(wordList)
for i in range(0, len(word), 2): ## Goes through the numbers between 0 and len(word)-1 with a step of two
word = word[ : i] + "-" + word[i + 1: ] ## Str assignment isn't supported so slicing is the best way to do this
Another implementation (with O.O's algorithm) is this:
import random
word = random.choice(wordList)
new = ''
for i in range(len(word)):
if i%2 == 1: ## If the index is odd (which is when you want the letter to be retained)
new += word[i]
else:
new += "-"
Upvotes: 0
Reputation: 868
You could use a for loop as follows to get each letter in the string one by one:
for character in string:
Then create a new string where for every other you either append
the '-' or the character
.
Upvotes: 0