Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does % do to strings in Python?

I have failed to find documentation for the operator % as it is used on strings in Python. What does this operator do when it is used with a string on the left hand side?

like image 225
Ram Rachum Avatar asked Aug 06 '09 11:08

Ram Rachum


People also ask

What does * do to strings in Python?

Joining of two or more strings into a single one is called concatenation. The + operator does this in Python. Simply writing two string literals together also concatenates them. The * operator can be used to repeat the string for a given number of times.

What does [- 1 :] mean in Python?

Python also allows you to index from the end of the list using a negative number, where [-1] returns the last element. This is super-useful since it means you don't have to programmatically find out the length of the iterable in order to work with elements at the end of it.

What does %d and %s do in Python?

They are used for formatting strings. %s acts a placeholder for a string while %d acts as a placeholder for a number. Their associated values are passed in via a tuple using the % operator.

What does %s mean in Python string?

The %s operator is put where the string is to be specified. The number of values you want to append to a string should be equivalent to the number specified in parentheses after the % operator at the end of the string value. The following Python code illustrates the way of performing string formatting.


1 Answers

It's the string formatting operator. Read up on string formatting in Python.

format % values 

Creates a string where format specifies a format and values are the values to be filled in.

like image 106
Konrad Rudolph Avatar answered Sep 20 '22 13:09

Konrad Rudolph