Open In App

Python Modulo String Formatting

Last Updated : 12 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Python, a string of required formatting can be achieved by different methods. Some of them are; 1) Using % 2) Using {} 3) Using Template Strings In this article the formatting using % is discussed. The formatting using % is similar to that of ‘printf’ in C programming language. %d – integer %f – float %s – string %x – hexadecimal %o – octal The below example describes the use of formatting using % in Python. 

Python3




# Python program to demonstrate the use of formatting using %
  
# Initialize variable as a string
variable = '15'
string = "Variable as string = %s" %(variable)
print (string )
  
# Printing as raw data
# Thanks to Himanshu Pant for this
print ("Variable as raw data = %r" %(variable))
  
# Convert the variable to integer
# And perform check other formatting options
  
variable = int(variable) # Without this the below statement
                        # will give error.
string = "Variable as integer = %d" %(variable)
print (string)
print ("Variable as float = %f" %(variable))
  
# printing as any string or char after a mark
# here i use mayank as a string
print ("Variable as printing with special char = %c" %(variable))
  
print ("Variable as hexadecimal = %x" %(variable))
print ("Variable as octal = %o" %(variable))


Output : 

Variable as string = 15
Variable as raw data = '15'
Variable as integer = 15
Variable as float = 15.000000
Variable as printing with special char = mayank
Variable as hexadecimal = f
Variable as octal = 17

This article is contributed by Nikhil Kumar Singh (nickzuck_007)


Previous Article
Next Article

Similar Reads

Python String Formatting - How to format String?
String formatting allows you to create dynamic strings by combining variables and values. In this article, we will discuss about 5 ways to format a string. You will learn different methods of string formatting with examples for better understanding. Let's look at them now! How to Format Strings in PythonThere are five different ways to perform stri
8 min read
JSON Formatting in Python
JSON (JavaScript Object Notation) is a popular data format that is used for exchanging data between applications. It is a lightweight format that is easy for humans to read and write, and easy for machines to parse and generate. Python Format JSON Javascript Object Notation abbreviated as JSON is a lightweight data interchange format. It encodes Py
3 min read
Python code formatting using Black
Writing well-formatted code is very important, small programs are easy to understand but as programs get complex they get harder and harder to understand. At some point, you can’t even understand the code written by you. To avoid this, it is needed to write code in a readable format. Here Black comes into play, Black ensures code quality. What is B
3 min read
Formatting Axes in Python-Matplotlib
Matplotlib is a python library for creating static, animated and interactive data visualizations. Note: For more information, refer to Introduction to Matplotlib What is Axes? This is what you think of as 'plot'. It is the region of the image that contains the data space. The Axes contains two or three-axis(in case of 3D) objects which take care of
4 min read
Formatting containers using format() in Python
Let us see how to format containers that were accessed through __getitem__ or getattr() using the format() method in Python. Accessing containers that support __getitem__a) For Dictionaries C/C++ Code # creating a dictionary founder = {'Apple': 'Steve Jobs', 'Microsoft': 'Bill Gates'} # formatting print('{f[Microsoft]} {f[Apple]}'.format(f = founde
1 min read
Python - Split strings ignoring the space formatting characters
Given a String, Split into words ignoring space formatting characters like \n, \t, etc. Input : test_str = 'geeksforgeeks\n\r\\nt\t\n\t\tbest\r\tfor\f\vgeeks' Output : ['geeksforgeeks', 'best', 'for', 'geeks'] Explanation : All space characters are used as parameter to join. Input : test_str = 'geeksforgeeks\n\r\\nt\t\n\t\tbest' Output : ['geeksfor
3 min read
Paragraph Formatting In Python .docx Module
Prerequisite: Working with .docx module Word documents contain formatted text wrapped within three object levels. The Lowest level- run objects, middle level- paragraph objects, and highest level- document object. So, we cannot work with these documents using normal text editors. But, we can manipulate these word documents in python using the pytho
9 min read
Hover Text and Formatting in Python-Plotly
Prerequisites: Python Plotly In this article, we will explore how to Hover Text and Formatting in Python. It is a useful approach to Hover Text and Formatting as it allows to reveal a large amount of data about complex information. One of the most deceptively-powerful features of data visualization is the ability for a viewer to quickly analyze a s
2 min read
Formatting Dates in Python
In different regions of the world, different types of date formats are used and for that reason usually, programming languages provide a number of date formats for the developed to deal with. In Python, it is dealt with by using a liberty called DateTime. It consists of classes and methods that can be used to work with data and time values. Require
5 min read
How to Print a Tab in Python: Enhancing Text Formatting
Python provides various formatting options to enhance the appearance of the text output. One common requirement is to insert tabs into printed text for better alignment or readability. This article explores different methods to print tabs in Python allowing developers to format text effectively. How to Print a Tab in Python: Enhancing Text Formatti
2 min read
Article Tags :
Practice Tags :