Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9194471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:25:18+00:00 2026-06-17T21:25:18+00:00

When doing string formatting in Python, I noticed that %s transforms also numbers to

  • 0

When doing string formatting in Python, I noticed that %s transforms also numbers to strings.

>>> a = 1
>>> b = 1.1
>>> c = 'hello'
>>> print 'Integer: %s; Float: %s; String: %s' % (a, b, c)
Integer: 1; Float: 1.1; String: hello

I don’t know for other variable types, but is it safe to use %s like this?

It is certainly quicker than specifying always the type each time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:25:19+00:00Added an answer on June 17, 2026 at 9:25 pm

    using %s automatically calls str on the variable. Since everything has __str__ defined, you should be able to do this without a problem (i.e. no exception will be raised). However, what you actually will have printed is another story …

    Note that in newer python code, there’s another option which uses the format method:

    'Integer: {}; Float: {}; String: {}'.format(a,b,c)
    

    This works basically the same way except that it is more powerful once you learn the syntax.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im doing a string formatting for a python program however, i am getting an
Python is doing string multiplication where I would expect it to do numeric multiplication,
I have a string in my cocoa GUI that needs to have special formatting
In python, is it possible to have a reoccurring string-formatting operation take place when
string str = 'my {0} long string {1} need formatting'; Should I be doing
How can I perform multiple formatting to a string? for e.g. doing in the
I would like to do some simple math while I'm doing string formatting. For
I have a server side Python script that returns a JSON string containing parameters
Is there any better alternative for doing string formatting in VC6, with syntax checking
For doing string concatenation, I've been doing basic strcpy , strncpy of char* buffers.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.