Python short course: String formatting output

 String in Python also support formatting output like below:



In [1]: '%s, eggs, and %s' % ('spam', 'SPAM')
Out[1]: 'spam, eggs, and SPAM'

In [2]: '{0}, eggs, and {1}'.format('spam', 'SPAM')
Out[2]: 'spam, eggs, and SPAM'

In [3]: '{}, eggs, and {}'.format('spam', 'SPAM')  #numbers are optional
Out[3]: 'spam, eggs, and SPAM'

Comments

Popular posts from this blog

Research from the Lab's Little AI Scholars: Learn the life language using artificial intelligence

Lab's recommendation: Awesome books to learn machine learning and AI (continue updating)

Lab's weekly topic - week 49, 2023 - What's limitations and possible solutions for a new AI tools to detect blood poisoning?