- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to compare Python string formatting: % with .format?
% can either take a variable or a tuple. So you'd have to be very explicit about what you want it to do. For example, if you try formatting such that −
my_tuple = (1, 2, 3) "My tuple: %s" % my_tuple You'd expect it to give the output: My tuple: (1, 2, 3)
But it will throw a TypeError. To guarantee that it always prints, you'd need to provide it as a single argument tuple as follows −
"hi there %s" % (name,) # supply the single argument as a single-item tuple
Remembering such caveats every time is not that easy and may cause bugs. .format doesn't have those issues. format is also very clean looking comparatively.
- How to compare two dates in String format in Java?
- String Formatting in Python using %?
- String Formatting Operator in Python
- String Formatting with ToString in C#
- How to compare string and number in Python?
- Formatting Minute in m format in Java
- Formatting day in d format in Java
- Formatting day in dd format in Java
- How to print a complete tuple in Python using string formatting?
- How does string formatting work in PowerShell?
- Python - Output Formatting
- String Formatting in C# using %
- String Formatting in Java using %
- How to format string using PowerShell?