Reputation: 55
Before you post the link to best way to remove punctuation.
I am creating a madLib game that replaces words in a paragraph with nouns,adverbs,verbs,adjectives etc; it is supposed to take a random word from a seperate file and print it to the paragraph where appropriate ie a verb running would be put into the postion where the paragraph states VERB. The only problem I'm having is that I am unable to do this when there is punctuation beside the word I am to replace. Such as VERB, or VERB!
My question is how do I replace all those values while keeping the punctuation there.
Upvotes: 2
Views: 776
Reputation: 89527
string.punctuation contains following characters:
'!"#$%&\'()*+,-./:;<=>?@[\]^_`{|}~'
You can use translate and maketrans functions to map punctuations to empty values (replace)
import string
'This, is. A test! VERB! and VERB,'.translate(str.maketrans('', '', string.punctuation))
Output:
'This is A test VERB and VERB'
Upvotes: 0
Reputation: 25
The sub function works fine for your problem
from re import *
contents = 'The !ADJECTIVE! panda walked to the !NOUN? and then *VERB!. A nearby <NOUN> was unaffected by these events.'
print('Enter an adjective: ', end = '')
adj = input()
print('Enter a noun: ', end = '')
noun1 = input()
print('Enter a verb: ', end = '')
verb = input()
print('Enter a noun: ', end = '')
noun2 = input()
contents = sub(r'adjective',adj,contents,count = 1, flags = IGNORECASE)
contents = sub(r'noun',noun1,contents,count = 1, flags = IGNORECASE)
contents = sub(r'verb',verb,contents,count = 1, flags = IGNORECASE)
contents = sub(r'noun',noun2,contents,count = 1, flags = IGNORECASE)
The sub function takes five arguments. re.sub(expression to find, string to replace, string in which replacement is done, count i.e, the number of occurrences should be replaced, IGNORECASE find all cases irrespective of upper or lower case) Output of the code
Enter an adjective: silly
Enter a noun: chandelier
Enter a verb: screamed
Enter a noun: pickup truck
The !silly! panda walked to the !NOUN? and then *VERB!. A nearby <NOUN> was
unaffected by these events.
The !silly! panda walked to the !chandelier? and then *VERB!. A nearby <NOUN> was
unaffected by these events.
The !silly! panda walked to the !chandelier? and then *screamed!. A nearby <NOUN> was
unaffected by these events.
The !silly! panda walked to the !chandelier? and then *screamed!. A nearby <pickup truck> was
unaffected by these events.
The punctuation marks are unaffected by these events. Hope this helps
Upvotes: 0
Reputation: 37249
Not sure of your use case, but does replace
with the count
parameter set to 1 work?
>>> test = 'This is a VERB! Whoa, a VERB? Yeah, a VERB!#$%'
>>> test.replace('VERB', 'running', 1)
'This is a running! Whoa, a VERB? Yeah, a VERB!#$%'
>>> test.replace('VERB', 'running', 1).replace('VERB', 'swimming', 1).replace('VERB', 'sleeping', 1)
'This is a running! Whoa, a swimming? Yeah, a sleeping!#$%'
Naturally you'd have to do some adjustments for the number of repetitions, but it should handle the punctuation fine.
As per @mgilson's suggestion below, you could remove the numerous calls to replace
by doing something like:
In [14]: s = 'This is a VERB! Whoa, a VERB? Yeah, a VERB!#$%'
In [15]: verbs = ['running', 'jumping', 'swimming']
In [16]: reduce(lambda x, y: x.replace('VERB', y, 1), verbs, s)
Out[16]: 'This is a running! Whoa, a jumping? Yeah, a swimming!#$%'
This uses the reduce
function to run replace
on the main string, using the values in verbs
as the ones to replace. The final argument to reduce is the string itself, which will contain the result of the replacement on each iteration (and will be the 'normal' string at the outset).
Upvotes: 2
Reputation: 6571
Use the sub function from the re module. Capture the character after the word, then substitute the word with the new word and append the captured punctuation using a backreference:
>>> import re
>>> s = "VERB,"
>>> print re.sub(r'VERB([\,\!\?\;\.]?)', r'newword\1', s)
newword,
You can expand the character class [\,\!\?\;\.]
to include whatever punctuation you expect to encounter, this is just an example.
Upvotes: 0
Reputation: 39698
noun1="Donkey"
print("This should print a %s here"%(noun1))
Essentially, you can get your input variables, and treat them like this example.
Upvotes: 2