Python Concatenation and Formatting

Photo by Shahadat Rahman on Unsplash
>>> print('apple' + 'banana')
applebanana
>>> print('apple', 'banana')
apple banana
>>> print('apple'+3)
Traceback (most recent call last):
File '', line 1, in
----> print('apple'+3)
TypeError: can only concatenate str (not "int") to str
>>> print('apple'+str(3))
apple3
>>> print('apple', 3)
apple 3
>>> a = 'apple'
>>> b = 'banana'
>>> print('This is %s and %s' % (a, b))
This is apple and banana
>>> a = 'apples'
>>> n = 3
>>> print('I have %i %s' % (n, a))
I have 3 apples
>>> a = 2.2145125621245
>>> print('Shorter float number %.2f' % a )
Shorter float number 2.21
>>> a = 'apple'
>>> b = 'banana'
>>> print('This is {} and {}'.format(a, b))
This is apple and banana
>>> a = 'apples'
>>> n = 3
>>> print('I have {} {}'.format(n, a))
I have 3 apples
>>> a = 2.2145125621245
>>> print('Shorter float number {:.2f}' .format(a))
Shorter float number 2.21

--

--

--

Software Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Amazon Reinvent in 4 mins : Top 8 things you should know

Controlling the Serial and Parallel Test on XUnit

6 Key Advantages of Laravel Framework for Website Development

Am I Too Young To Be a Senior Developer?

Child sitting at a desk, coding

Leo TechnoSoft (LTS) is leader in providing Right-shore Captive Team for ISV’s and Start-ups.

Start a static HTTP web server

Lines of code on a MacBook

MetaWallet 0.9.9 Beta.

Simple is Beautiful

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adi Ramadhan

Adi Ramadhan

Software Engineer

More from Medium

The OOPs factor in python

Python Tutorials Programs for Beginners: Part 1 (Basic Syntax)

PYTHON 101: THE ULTIMATE PYTHON TUTORIAL FOR BEGINNERS.

Git commands cheatsheet for your next interview