What s the difference between s and d in Python string formatting

0 votes
What us the difference between %s and %d how do they work?
Feb 3, 2022 in Python by Nandini
• 5,480 points
22,909 views

2 answers to this question.

0 votes

In Python both %s and %d are placeholders for a string and a number respectively.

name = 'Robey’
number = 454
print '%s %d' % (name, number)

%s will return the string and %d will return number, the values are passed using % operator. This % operator formatting is used in C language also.
 

answered Feb 3, 2022 by Dev
• 6,000 points
0 votes

The reason is that they are using this for formatting the strings. The%s acts a placeholder for a string while %d acts as a placeholder for a number. The associated values of them are then passed in through a tuple using the % operator.

name = 'marcog'
number = 42 
print '%s %d' % (name, number)

The above information will print marcog 42. However, please note that the name is a string (%s) and number is an integer (%d for decimal).

In Python 3 the example would be:
 

print('%s %d' % (name, number))

answered Feb 8, 2022 by Rahul
• 9,680 points

Related Questions In Python

+1 vote
1 answer

What's the difference between eval, exec, and compile in Python?

exec is not an expression: a statement ...READ MORE

answered Aug 28, 2018 in Python by Priyaj
• 58,020 points
5,112 views
0 votes
1 answer

What's the difference in Qt between setVisible, setShown and show/hide

show() is just a convenience function for ...READ MORE

answered Apr 17, 2018 in Python by anonymous
8,058 views
+1 vote
1 answer

What is the difference between range and xrange functions in Python 2.X?

xrange only stores the range params and ...READ MORE

answered Aug 22, 2018 in Python by Priyaj
• 58,020 points
2,435 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,464 views
0 votes
1 answer
0 votes
1 answer

What is the difference between sets and lists in Python?

Lists can contain duplicates but sets cannot Sets ...READ MORE

answered Feb 9, 2022 in Python by Dev
• 6,000 points
1,171 views
0 votes
1 answer

Converting integer to string in Python

In order to convert an object to string ...READ MORE

answered Feb 11, 2022 in Python by Dev
• 6,000 points
602 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP