Skip to main content

All Questions

0 votes
1 answer
270 views

New style format specifier throwing tokenization error

I have the below string in the variable jss. jss = ''' Java.perform( function() { var item = Java.use("java.util.Random"); console.log("HOOKING random"); item.nextInt.overload("int")....
qre0ct's user avatar
  • 6,222
5 votes
2 answers
2k views

Is it ever appropriate to join two strings using the plus sign (+) over concatenating with curly brackets ({}) and `format` in Python 2.7?

I'm trying to write clean and concise code, and in a lot of code I look over sometimes I see people are inconsistent in their code. What I'm asking is, is there ever an instance where this print("...
Craig's user avatar
  • 593
1 vote
3 answers
399 views

Why is str.format() better than str()?

To convert a number into a string in python I used the str() function, but a few people suggested that I use format() instead. When I tried both, I got the same result: n = 10 print ['{}'.format(n)]...
Deba's user avatar
  • 609
0 votes
1 answer
87 views

Apply decimal formatting for a decimal in a sentence of strings + numbers

I am trying to apply decimal formatting for a decimal in a string sentence (The temperature of $place is $value degree $unit) which is converted into (The temperature of room is 45 degree Celsius) I ...
J2G's user avatar
  • 17
0 votes
1 answer
718 views

Concatenate string using .format

I have some code similar to the following: test_1 = 'bob' test_2 = 'jeff' test_1 += "-" + test_2 + "\n" Output: bob- jeff\n I'd like to have the same functionality but using the .format method. ...
Proletariat's user avatar
3 votes
4 answers
4k views

Why don't string variables update when they are changed

Say we have some code like so : placehold = "6" string1 = "this is string one and %s" % placehold print string1 placehold = "7" print string1 When run, both of the print statements return as if ...
John Hon's user avatar
  • 213
3 votes
3 answers
434 views

Python Str to Int - Very Simple but tricky [duplicate]

I have a small problem, in my GUI application. Which takes only strings to display. I have to perform the following logic. I have a string 'a', which are numbers. It is five digits. I have to ...
Jesh Kundem's user avatar
1 vote
2 answers
42 views

Python Change how string is printed

I have a string output from another program that shows the date as 16/05/03 # (YY/MM/DD) and I wish to change it to 03/05/16 #(DD/MM/YY) and here is how the date is supplied (date = info[4]....
shaggs's user avatar
  • 620
0 votes
0 answers
134 views

Multiple specification in python string format

I want to specify a certain width for my string and also specify the number of digit. I can each of these formatting separately: print '{:13}'.format(1.0/3) print '{:.2f}'.format(1.0/3) I could ...
DevShark's user avatar
  • 9,146
3 votes
1 answer
128 views

Can a variable be passed to format() to pad?

With new-style formatting, we can do: In [262]: '{:_>10}'.format('test') Out[262]: '______test' Instead of the underscore (or whatever character), can this be replaced by a variable? So if: ...
Pyderman's user avatar
  • 16.3k
9 votes
3 answers
4k views

Python string formatting - old `%` vs new `str.format`

New formatting lets us do this: '{:.<12}'.format('##') - optional fill character. Can we do that using old formatting? (I know we can fill with spaces '%-12s' % '##' ) Also, old formatting lets us ...
avamsi's user avatar
  • 399
3 votes
3 answers
143 views

Parsing text with Python 2.7

Text File • I.D.: AN000015544 DESCRIPTION: 6 1/2 DIGIT DIGITAL MULTIMETER MANUFACTURER: HEWLETT-PACKARDMODEL NUM.: 34401A CALIBRATION - DUE DATE:6/1/2016 SERIAL NUMBER: MY45027398 • I.D.: ...
Kehlin Swain's user avatar
1 vote
1 answer
63 views

Can someone please explain these Python string formatters

I am confused at the difference between the Python string formatters %d and %i. The Python manual page explains them as the same thing, 'signed integer decimal' - confused ! From the Python 'Built in ...
ilium007's user avatar
  • 589
2 votes
4 answers
105 views

Use parameter in format string

This: print '{:x<4d}'.format(34) prints this: 34xx How can I do this: width = 13 print '{:x<|width|d}'.format(34) to get this: 34xxxxxxxxxxx
Sam R.'s user avatar
  • 16.4k
0 votes
0 answers
93 views

How to make a logger work with string.format instead of string formatters [duplicate]

Right now I have a lot of code that uses the traditional C-style string formatters, i.e. print "Hi, %s" % name We're working on transitioning over to using string.format(), i.e. print "Hi, {}"....
Dan Oberlam's user avatar
  • 2,496

15 30 50 per page