Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python, format string

Tags:

python

I am trying to build a format string with lazy argument, eg I need smth like:

"%s \%s %s" % ('foo', 'bar') # "foo %s bar"

how can i do this?

like image 793
Anycorn Avatar asked Feb 08 '11 00:02

Anycorn


People also ask

Can you format a string in Python?

Python uses C-style string formatting to create new, formatted strings. The "%" operator is used to format a set of variables enclosed in a "tuple" (a fixed size list), together with a format string, which contains normal text together with "argument specifiers", special symbols like "%s" and "%d".

What does format () do in Python?

The format() method formats the specified value(s) and insert them inside the string's placeholder. The placeholder is defined using curly brackets: {}. Read more about the placeholders in the Placeholder section below. The format() method returns the formatted string.

What is %s and %D in Python?

%s is used as a placeholder for string values you want to inject into a formatted string. %d is used as a placeholder for numeric or decimal values. For example (for python 3) print ('%s is %d years old' % ('Joe', 42)) Would output Joe is 42 years old.

What does .2f mean in Python?

A format of . 2f (note the f ) means to display the number with two digits after the decimal point. So the number 1 would display as 1.00 and the number 1.5555 would display as 1.56 .


1 Answers

"%s %%s %s" % ('foo', 'bar')

you need %%

like image 194
Foo Bah Avatar answered Sep 20 '22 16:09

Foo Bah