Python String Formatting

Photo by Fabio Santaniello Bruun on Unsplash
name = “Annika”print(“Hello, “, name)result:"Hello, Annika"
name = “Annika”greeting = “Hello, %s.” % nameprint(greeting)result:“Hello, Annika”
name = "Annika"print(“Hello, {}”.format(name))result: "Hello, Annika"
plan = {“name”:”Annika”,”activity”:”run”,”pet_name”:”Piper”}print(“Good Morning, {name}! Let’s go for a {activity} today with {pet_name}.”.format(**plan))result:"Good Morning, Annika! Let's go for a run today with Piper."
name= “Annika”activity = “run”pet_name = “Piper”print(“Good Morning, {}! Let’s go for a {} today with {}.”.format(activity, pet_name, name)result:“Good Morning, run! Let’s go for a Piper today with Annika.”
print(f”Good Morning, {name}! Let’s go for a {activity} today with {pet_name}.”)result:“Good Morning, Annika! Let’s go for a run today with Piper.”
def shout(text):
return text.upper()
f"{shout(activity)}"result:
"RUN"
Code for comparing times of 3 methods
Using the %s operator: 0.002892422489821911Using the format method: 0.006621009670197964Using the f-string: 0.0013131508603692055

--

--

--

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

Recommended from Medium

DEFIROCK x IM community AMA RECAP

Maximum Width Ramp Problem

Connecting Microsoft SQL Server Integration Services and Microsoft SQL Server Data Tools to Oracle…

The Aun Collective Archive — Weekly Update (TACA-WU #0002)

No Bugs Allowed

How to Deploy Your Portfolio with GitHub Pages and Host on GoDaddy

GCP Certification Series: 1.2, Managing billing configuration

Alvin’s Dew Drop Daily — Issue #83

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
Annika Noren

Annika Noren

More from Medium

Map : Python Basics

Introduction to Python!

DICTIONARY IN PYTHON

The helicopter view of Python (for intermediate) part 1