In this tutorial, we’ll learn how to easily format string in Python. We’ll going through multiple methods and examples to understand how to practically format string in Python.
Outline
- Introduction: Format String In Python
- 3 Ways To Format String In Python
- Method 1: % Operator
- Method 2: Python String format Method
- Method 3: f-String
- Conclusion
Introduction: Format String In Python
It specifies adding new data dynamically in a string with proper formats. Let’s discuss about the multiple methods which can be used to format string in Python.
3 Ways To Format String In Python
Let’s discuss about 3 different ways for string formatting in Python programming language. These are listed below:
- % Operator
- Python String format() Method
- f-String
Method 1: % Operator
This symbol % is also known as ‘string formatting operator’. Its an old method to format a Python string. See below example:
print('This is %s Programming' %'Python')
Output
This is Python Programming
We’ve specified a string ‘Python’ and have used it inside the main string using %s.
- %s is for strings
- %d is for integers
- %f is for floating point values
- %b is for binary format
We can also inject multiple values. See below examples:
print('This is %s Programming %s' %('Python','Language')) print('This is %s Programming %d' %('Python',3)) print('This is %s %d %s' %('Python',3,'Programming')) print('This is Python Programming %d' %3) print('This is %s Programming %f' %('Python',3.0)) name='Python' print('This is %s %d' %(name,3))
Output
This is Python Programming Language This is Python Programming 3 This is Python 3 Programming This is Python Programming 3 This is Python Programming 3.000000 This is Python 3
Method 2: Python String format Method
This method can be used to inject data specified in it into main string using placeholders. These placeholders are specified using curly braces {}. See below examples:
print('This is Python Programming Language'.format('Python')) print('This is {} Programming {}'.format('Python','Language')) print('This is {}{} Programming {}'.format('Python',3,'Language')) print('{} {} {}{} {} {}'.format('This','is','Python',3,'Programming','Language')) name='Python' print('This is {} Programming {}'.format(name,'Language'))
Output
This is Python Programming Language This is Python Programming Language This is Python3 Programming Language This is Python3 Programming Language This is Python Programming Language
Method 3: f-String
In this method, we’ve to use f character at the beginning of a string and to inject data, we’ve to use curly braces. In this method, order is not important. See below examples:
a='Programming' b='Python' print(f'This is {b} {a}') name='Python' number=3 print(f'{name} Programming language {number}') num1=3 num2=4 print(f'The sum of {num1} and {num2} is {num1+num2}')
Output
This is Python Programming Python Programming language 3 The sum of 3 and 4 is 7
These are the multiple ways through which we can format string in Python. Click here if you want to learn more. Hope you like this post.
Don’t hesitate to ask if you still have any questions regarding how to format string in Python. I’ll be very glad to answer all your questions.
Conclusion
To conclude this tutorial, hope you now have a detailed practical understanding of how to properly format string in Python. I’ll be very happy to receive your feedback on this post.
Do visit my other articles on Python programming. Links to some of them are listed below. Others can be found using the navigation bar or search box of this site. Thank you for reading this post.
You may like to read:
How To Easily Reverse String In Python [Python Code Examples]
[Solved] How To Convert Python Tuple To String
How To Easily Get Input In Python [Python Code Examples]
[Solved] How To Find Maximum Value In Python Tuple
How To Easily Use Python String Split Method [Detailed Python Guide]
How To Find Minimum Value In Python Tuple [Detailed Python Guide]
How To Use Python String Title Method [Python Easy Guide]
How To Change Flutter CircularProgressIndicator Stroke Width – Easy Flutter Example Code
How To Properly Use Python String StartsWith Method [Detailed Python Guide]