in Education by
In Python 2.6, the str.format() has been introduced and has a slightly different syntax than existing % operator. When should I use which one and who's better? I tried this and used each method and the outcome I received is same, then what's the difference between two of them? #!/usr/bin/python sub1 = "an Amimal!" sub2 = "horn" q = "It's me %s" % sub1 w = "It's me {0}".format(sub1) e = "with big %(kwarg)s!" % {'kwarg':sub2} r = "with big {kwarg}!".format(kwarg=sub2) print q # "It's me an animal!" print w # "It's me an animal!" print e # "with big horn!" print r # "with big horn!" I am also confused when string formatting occurs in Python? Is this happen when my logging level is set to High will I still have a problem while performing the following % operation? And if this is true than how can I avoid this? log.debug("some debug info: %s" % some_info) Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
% either takes a variable or a tuple whereas .format don't have these issues, for instance check this: "hi there %s" % name for your second doubt, I assume you're using logging module , then try this: log.debug("some debug info: %s", some_info) This will avoid all formatting unless your logger logs something.

Related questions

0 votes
    y = " \{ Hi\} {0} " print y.format(43) it's showing an error: Key Error: Hi\\ I want to print the output from the above syntax: {Hi} 43 Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    How can I convert a str from uppercase, or half uppercase to lowercase? E.x. "Decimeter" to "decimeter" Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    In Python, How to remove trailing and leading whitespace from str ? For instance check this: " Hey" --> "Hey" " ... "Harsh has a dog" Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    In Python how can I convert an integer to a string? I am trying this: r = 16 r.str() I am trying to ... but there's nothing like that. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    How can I generate a string of size N. I want it to be of uppercase English letters and numbers like this: 7I2D86 ... do it in Python. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    Can someone tell me how can I Call a function in a string with function name in python program? E.x. Let's ... to perform this task. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    Can someone tell me a method to check how can I represent a string as a number in Python? I am using this ... please help me with it. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I am confused about the usage of string.join(list) instead of list.join(string), for instance see this code: ... reason behind this? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    Can someone tell me how to convert the bytes value back to string? I have used binascii.b2a_qp() method, ... doing it manually. Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I want to covert a large complex date-time string into proper datetime fields in a database, Can someone tell me a ... can't use SQL. Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    How can someone parse a numeric string like "121.5555" to it's corresponding float value 121.5555? or parse a ... str to an int. Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I'm completely new in logging but familiar in SQL queries. I'm now experimenting with Serilog and Seq. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    Is there a built-in that removes duplicates from a list in Python, whilst preserving order? I know that I can ... idiom if possible. Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    What is the meaning of using leading underscores before the name of an object in Python? And what's the difference ... , method, etc.? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I want to write a program which returns different values for different input,it can be easily done using switch ... some alternative? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
...