4

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

u'Your new contact email [email protected] 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 e.g.

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

And somehow pass address into the variable at runtime?

2
  • 3 upvotes??? You're using the perfect word here: "string formatting". So, you couldn't find anything related to it by doing simple Google search? May be I overlooked something. Commented Oct 13, 2015 at 5:48
  • @AshwiniChaudhary I already know about string formatting and how it is used. That's not the issue. What I'm looking to do is somehow set up a string variable in such as way that it is going to allow another string to be plugged into it at runtime. The key point here is that I wish to declare sentence globally at the outset, not after address has come into existence. Commented Oct 13, 2015 at 5:49

3 Answers 3

2

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!

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but the thing is I'm looking to declare the variable sentence well before address exists. More like a constant, really.
Also see my clarifying reply to Ashwini.
@Pyderman: You can do that, too! I'll update my answer to show you.
1

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).

Comments

1

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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.