简体   繁体   中英

How to account for string formatting in Python variable assignment?

I am parsing text to check for the presence such as:

u'Your new contact email thedude@gmail.com has been confirmed.'

...where the text either side of the email address will be constant, and the email address won't be constant, but will be known before parsing.

Assume the sentence is contained in a variable called response and the email address in address . I could do:

'Your new contact email' + address + 'has been confirmed' in response

This is a little untidy, and downright inconvenient if the text of the sentence ever changes. Is it possible to advantage of string formatting in a variable assignment eg

sentence = 'Your new contact email %s has been confirmed'

And somehow pass address into the variable at runtime?

Of course you can! Try this out...

sentence = 'Your new contact email {} has been confirmed'.format(address)

There's also this other (rather hacky) alternative...

sentence = 'Your new contact email %s has been confirmed' % address

This alternative has its limitations too, such as requiring the use of a tuple for passing more than one argument...

sentence = 'Hi, %s! Your new contact email %s has been confirmed' % ('KemyLand', address)

Edit : According to comments from the OP, he's asking how to do this if the format string happens to exist before address does. Actually, this is very simple. May I show you the last three examples with this?...

# At this moment, `address` does not exist yet.

firstFormat = 'Your new contact email address {} has been confirmed'
secondFormat = 'Your new contact email address %s has been confirmed'
thirdFormat = 'Hi, %s! Your new contact email %s has been confirmed'

# Now, somehow, `address` does now exists.

firstSentence = firstFormat.format(address);
secondSentence = secondFormat % address
thirdSentence = thirdFormat % ('Pyderman', address)

I hope this has led some light on you!

Maybe a hack way of doing but if I understand you correctly, here's how you can..

At the beginning, declare the string, but where the address would go, put in something that will never generally be repeated... Like ||||| (5 pipe characters).

Then when you have the address and want to pop it in do:

myString.replace('|||||', address)

That will slot your address right where you need it :)

My understanding was you are trying to create a string and then later, add a piece in. Sorry if I misunderstood you :)

This is what I usually do with my SQL queries, output lines and whatever:

sentence = 'Blah blah {0} blah'
...
if sentence.format(adress) in response:
    foo()
    bar()

So basically you get to keep all your I/O-related strings defined in one place instead of hardcoded all over the program. But at the same place you get to edit them whenever you please, but only in a limited way ('foo'.format() throws an exception when it gets too few or too many arguments).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM