To append a string to another in Python, use the += operator. Python += operator appends a string to another. It adds two values together and assigns the final value to a variable.
To use formatted string literals, begin a string with f or F before the opening quotation mark or triple quotation mark. Inside this string, you can write a Python expression between { and } characters that can refer to variables or literal values.
Use the str. zfill() Function to Display a Number With Leading Zeros in Python.
Python provides different ways and methods to generate a substring, to check if a substring is present, to get the index of a substring, and more. start - The starting index of the substring. stop - The final index of a substring. step - A number specifying the step of the slicing.
Python has a %
operator for this.
>>> a = 5
>>> b = "hello"
>>> buf = "A = %d\n , B = %s\n" % (a, b)
>>> print buf
A = 5
, B = hello
>>> c = 10
>>> buf = "C = %d\n" % c
>>> print buf
C = 10
See this reference for all supported format specifiers.
You could as well use format
:
>>> print "This is the {}th tome of {}".format(5, "knowledge")
This is the 5th tome of knowledge
If I understand your question correctly, format() is what you are looking for, along with its mini-language.
Silly example for python 2.7 and up:
>>> print "{} ...\r\n {}!".format("Hello", "world")
Hello ...
world!
For earlier python versions: (tested with 2.6.2)
>>> print "{0} ...\r\n {1}!".format("Hello", "world")
Hello ...
world!
I'm not completely certain that I understand your goal, but you can use a StringIO
instance as a buffer:
>>> import StringIO
>>> buf = StringIO.StringIO()
>>> buf.write("A = %d, B = %s\n" % (3, "bar"))
>>> buf.write("C=%d\n" % 5)
>>> print(buf.getvalue())
A = 3, B = bar
C=5
Unlike sprintf
, you just pass a string to buf.write
, formatting it with the %
operator or the format
method of strings.
You could of course define a function to get the sprintf
interface you're hoping for:
def sprintf(buf, fmt, *args):
buf.write(fmt % args)
which would be used like this:
>>> buf = StringIO.StringIO()
>>> sprintf(buf, "A = %d, B = %s\n", 3, "foo")
>>> sprintf(buf, "C = %d\n", 5)
>>> print(buf.getvalue())
A = 3, B = foo
C = 5
Use the formatting operator %
:
buf = "A = %d\n , B= %s\n" % (a, b)
print >>f, buf
You can use string formatting:
>>> a=42
>>> b="bar"
>>> "The number is %d and the word is %s" % (a,b)
'The number is 42 and the word is bar'
But this is removed in Python 3, you should use "str.format()":
>>> a=42
>>> b="bar"
>>> "The number is {0} and the word is {1}".format(a,b)
'The number is 42 and the word is bar'
If you want something like the python3 print function but to a string:
def sprint(*args, **kwargs):
sio = io.StringIO()
print(*args, **kwargs, file=sio)
return sio.getvalue()
>>> x = sprint('abc', 10, ['one', 'two'], {'a': 1, 'b': 2}, {1, 2, 3})
>>> x
"abc 10 ['one', 'two'] {'a': 1, 'b': 2} {1, 2, 3}\n"
or without the '\n'
at the end:
def sprint(*args, end='', **kwargs):
sio = io.StringIO()
print(*args, **kwargs, end=end, file=sio)
return sio.getvalue()
>>> x = sprint('abc', 10, ['one', 'two'], {'a': 1, 'b': 2}, {1, 2, 3})
>>> x
"abc 10 ['one', 'two'] {'a': 1, 'b': 2} {1, 2, 3}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With