What the actual use of %s, %d, %r?


#1

@zeziba @factoradic @mtf

Hi
I want to ask whats the actually use of %s, %d, %r, I mean I know the usage of these 3 strings characters.. i.e.

%s - it is used for string presentation
for ex.:
string = "Hi I'm Pradeep, and I'm learning %s." % "python"
print string

Output:
Hi I'm Pradeep, and I'm learning python.

%d - it is used to display numbers
for ex.:
numb = "There are %d types of people." % 10
print numb

Output:
There are 10 types of people.

%r - it is used as string representation
for ex.:
micheal_jackson = "he is a great dancer."
string = "I said %r" %micheal_jackson
print string

Output:
I said he is a great dancer.

As far as I know how to use these string characters but my question is I don't when to use these string characters in real life coding....

Hope you can understand what I'm trying to ask??

--
Regards
Pradeep


#2

@arcjumper22033,

Do a google search

== the Book ==
python formatting output site:python.org

== discussions / opinions ==
python formatting output site:stackoverflow.com


#3

well this may be very unhelpful but I'm going to ask a question to your question... So i'm just learning about the use of %s %d %r and in 4.3 String Formatting with %, Part 1 it says:

string_1 = "Camelot"
string_2 = "place"

print "Let's not go to %s. 'Tis a silly %s." % (string_1, string_2)

its able to use %s instead of %r... so whats the point of %r


#4

@student_coder

Hi, %s is used for string characters, %d it is used for numbers, but %r is used for debugging as it contains the raw data of the variable. Let me explain you by an example.

var_a = "I'm learning %s, I' ve %d ways to do this exercise." % ("python", 10)
print var_a

Output:
I'm learning python, I' ve 10 ways to do this exercise.

print "Hi, I said %r" % (var_a)
Output:
Hi I said I'm learning python, I' ve 10 ways to do this exercise.

Hope this helps.


#5

thanks I understand now :smile: