Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python: Replace characters in string at different positions with arguments

It’s a simple question. I have a generic string and I want to replace an asterisk * with arguments provided.

'* is the capital of *'

If I shall give two arguments ('Berlin','Germany'), I should get

'Berlin is the capital of Germany'

It’s an easy problem, and I can solve it, but I am looking for one line solution where every positional argument replaces the corresponding *. I think, I have seen something of this type (*) ...(*), but can’t recollect. Someone has any idea?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You have many ways to do this, a simple one if you have tuples of 2 elements:

t = ('Berlin', 'Germany')
'{} is the capital of {}'.format(*t)

or

'%s is the capital of %s' % t

output: 'Berlin is the capital of Germany'

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading