Take the 2-minute tour ×

Am generating dummy email address for testing purpose, and so wanted the following code to work

return "test_%@test.tm"% counter();

but its not working, seems some special things about @, not able to have @ in string.


Could somebody please tell me why output has no 'email' attribute/key present here with this example - https://gist.github.com/4505055

btw Am new to python


my mistake, email attr was missing, sorry :(

share|improve this question
There's something special about %. Did you want "test_%s@test.tm"% counter()? –  Anton Kovalenko Jan 10 '13 at 19:11
%@t is an incorrect format specifier, that's why you're getting the error. –  Aशwini चhaudhary Jan 10 '13 at 19:13
@AmolPujari: btw, you can leave out the semicolons at the ends of lines... –  Ned Batchelder Jan 10 '13 at 19:14
add comment

closed as not a real question by Amol Pujari, Blair, bensiu, xpda, Rais Alam Jan 11 '13 at 4:42


5 Answers

up vote 3 down vote accepted

It seems that you forgot the string replacement character %s.

return "test_%s@test.tm" % counter()
share|improve this answer
add comment

That's because the character following the percent symbol in the string is supposed to specify what kind of value you're inserting in. For example, doing

"test_%i@test.tm" % 1

...would tell Python you want to insert an integer where '%i' is. However, '%@' isn't a recognized string insertion thing, so it won't work. More info

A slightly more robust way to do the same thing might be this:




More info

share|improve this answer
You might want to give docs.python.org/2/library/stdtypes.html#string-formatting as reference for the (obsolete) % syntax, as it's definetely „normative“. –  Jonas Wielicki Jan 10 '13 at 19:16
Thanks, I changed the link –  Michael0x2a Jan 10 '13 at 19:17
add comment

There's nothing special about the @. It seems you are doing string replacement, but you've provided an invalid value to be replaced.

Try this instead:

return "test_%s@test.tm"% counter();
share|improve this answer
add comment

you have a '%' character with your @ symbol after it. That is not a valid string conversion type.

See: http://docs.python.org/2/library/stdtypes.html#string-formatting-operations

perhaps you meant: 'test_%s@test.tm' % counter()

(also, you don't need to terminate statements with semicolon;)

share|improve this answer
add comment

You forgot the formatter after the percent sign in your string.

return "test_%s@test.tm"% counter()

Also you don't have to add semicolon at the end of statements in python. In fact python wont complain about it because it is valid, but not recommended.

share|improve this answer
add comment
