4 Ways How to Format String in Python Simple and Easy

4 Ways How to Format String in Python – Basically every time we learn a programming language the main thing to do is do a simple scripting or test script to bring up the output in the form of text that the program is running. “ Hello World” is familiar to which in general almost all programming languages use to describe the basic syntax of programming to illustrate how code/scripts work.

In the last 2 months, around the end of June 2021, I started learning the Python programming language. With time, it automatically finds errors, searching for references from various sources how to address errors when we test the program runs or not. To install python, pal of JudisWeb can visit the latest version of How to Install Python article on Android Smartphone.

How to Format String in Python

1. How to Format String in Python Using Concatenation: print(‘Name: ‘+name)

As on the basis of almost all programming languages “Hello World” is the mainstay of every programmer to take an initial test of how code/script works.

print ("Hello World")
print ("Bagus Judistirah")
print ("https://bjxbro.online")
Hello World
Bagus Judistirah
https://bjxbro.online

Yup, that’s the basis of print output on python, just as it does any other programming PHP for example using echo, basically the same just a different syntax.

<?php
	echo "Hello World";
?>

Then when you print on a different data type, an error will occur. And this is often asked in forums, groups or a discussion. I personally don’t have a laptop/notebook or PC, I only rely on a smartphone. Can you do programming with a smartphone? Of course you can and don’t deny that using only a smartphone there are many obstacles and limitations, such as a small screen, sometimes keyboard errors, difficulty copy-pasting-cut, etc. Of course, with these limitations it doesn’t make me back down, even in making this website 100% starting with VPS settings and so on using the power of smartphone.

Concatenation example:

name		= "Bagus"
address		= "Malang"
age			= 35

print ("Name		: " + name)
print ("Address	: " + address)
print ("Age		: " + str(age)) 
Name   	: Bagus
Address	: Malang
Age		: 35

If the age variable has an integer data type, then it must be converted to the string str (var_integer). If it is not revamped to a string then:

Traceback (most recent call last):
  File "main.py", line 7, in <module>
    print ("Age	: " + age)
TypeError: can only concatenate str (not "int") to str

Cara Mem-Format String pada Python Error

2. How to Format String in Python us%s string: print (‘Name: %s’% name)

nama		= "Bagus"
alamat		= "Malang"
umur		= 35

print ("Nama	: %s" % nama)
print ("Alamat	: %s" % alamat)
print ("Umur	: %s" % umur)

text = "Nama: %s, dengan Alamat: %s, dan berumur: %s tahun" % (nama, alamat, umur)

print (text) 
Nama: Bagus, dengan Alamat: Malang, dan berumur: 35 tahun

3. How to Format String in Python using Format: “.format ()

nama		= "Bagus"
alamat		= "Malang"
umur		= 35

text = "Nama: {}, dengan Alamat: {}, dan berumur: {} tahun".format(nama,alamat,umur)

print (text)
Nama: Bagus, dengan Alamat: Malang, dan berumur: 35 tahun


4. How to Format String in Python using F String: F’Name: {name} ‘

nama		= "Bagus"
alamat		= "Malang"
umur		= 35

text = f"Nama: {nama}, dengan Alamat: {alamat}, dan berumur: {umur} tahun"

print (text)
Nama: Bagus, dengan Alamat: Malang, dan berumur: 35 tahun

Conclusion

Of those 4 ways equally produce the same output in different ways. As a note on the way number 2, string format is used on Python2 but this is also true on python3. From the conclusion above How to Format Strings in Python in 4 ways is easy to learn.

nama		= "Bagus"
alamat		= "Malang"
umur		= 35

Cara1 = "Nama: " + nama + " dengan Alamat: " + alamat + " dan berumur: " + str(umur) 

Cara2 = "Nama: %s, dengan Alamat: %s, dan berumur: %s tahun" % (nama, alamat, umur)

Cara3 = "Nama: {}, dengan Alamat: {}, dan berumur: {} tahun".format(nama,alamat,umur)

Cara4 = f"Nama: {nama}, dengan Alamat: {alamat}, dan berumur: {umur} tahun"

Reference: Python Documentation

Leave a Comment