-4
color_list = ["Red","Green","White" ,"Black"]
print( "%s %s"`%(color_list[0],color_list[-1])`)

what does % operator referred in python. what is the purpose to use?

Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
  • 2
    check https://www.python-course.eu/python3_formatted_output.php `the modulo operator "%" is overloaded by the string class to perform string formatting.` You should use `f-string` which is more readable and faster. – Epsi95 Jul 20 '21 at 05:17
  • It is used to specify what kind of variable is going to be in the output at that place. It's equivalent of the `printf` function in C where you do something like `printf("%d",1)` which prints 1. You used %d to specify that you're expecting a decimal number. `%s` is used to specify that you're expecting a string there – RishiC Jul 20 '21 at 05:19
  • See this for more details https://stackoverflow.com/questions/5082452/string-formatting-vs-format-vs-f-string-literal – Joe Ferndz Jul 20 '21 at 05:19
  • Does this answer your question? [String formatting: % vs. .format vs. f-string literal](https://stackoverflow.com/questions/5082452/string-formatting-vs-format-vs-f-string-literal) – Joe Ferndz Jul 20 '21 at 05:19
  • 1
    It's an obsolete form of string formatting. The `print` isn't actually relevant – juanpa.arrivillaga Jul 20 '21 at 05:24

1 Answers1

0

check out the official python documentation

String Formatting

s % obj

mod(s, obj)

https://docs.python.org/3/library/operator.html

and don't confuse it with the modulo operator

Omjelo
  • 109
  • 8