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, Indonesia

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

Recommended from Medium

Introducing YSQL: A PostgreSQL Compatible Distributed SQL API for YugaByte DB

The story of my Bootcamp team

John Lewis Partnership — Measuring Our Delivery Performance Using DORA Metrics

Jenkins Declarative Pipeline Essentials

Laravel interview questions and answers 2017 — Laravel Interview Questions

FOSSASIA interview with founder Hong Phuc Dang

How do you turn regulatory requirements into user stories?

Amazon Athena- The New Serverless Data Analytics Tool

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, Indonesia

More from Medium

Check if a number is between two values using python

What are Iterators, Generators And Decorators in Python?

A tutorial on Python while loops

Data types in Python in 2 minutes with examples