5

Title says it all, I need to log two strings in one line.
For example, something like this:

logging.info(string1,string2)

Thank you :)

0

5 Answers 5

17

The logging functions act like this:

result = arg1 % (arg2, arg3, ...)

What you have will try to format the first string with the second string:

result = string1 % string2

Either manually specify a formatting string:

logging.info('%s %s', string1, string2)

Or join them together into one string:

logging.info(' '.join([string1, string2]))
Sign up to request clarification or add additional context in comments.

1 Comment

You forgot the simple case of string1 + string2.
3

You can use %-formatting, like this:

logging.info("%s %s", string1, string2)

For example:

 >>> string1, string2 = "spam", "pie"
 >>> logging.info("%s %s", string1, string2)
 INFO: spam pie

Comments

2
>>> ( s1,s2 ) = ( 'apple','mango' );
>>> print( '{},{}'.format(s1,s2) );
apple,mango

Comments

2

Using f strings, you could turn this into a one-liner and make it quite readable.

# python3 f-strings
logging.info(f'{string1} {string2}')

1 Comment

Not a good practice to do so. For more details, check palkeo.com/en/blog/python-logging.html
0
logger.info("String 1: {0!r} --- String 2: {1!r}".format(string1, string2))

Demo:

>>> print "String 1: {0!r} --- String 2: {1!r}".format('hello', 'world')
String 1: 'hello' --- String 2: 'world'
>>> print "String 1: {1!r} --- String 2: {0!r}".format('world', 'hello')
String 1: 'hello' --- String 2: 'world'

Two things to note here:

  1. The number in {0!r} and {1!r} specify the argument you get from input.
  2. '!s' (apply str()) and '!r' (apply repr()) can be used to convert the value before it is formatted.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.