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 7868783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:06:36+00:00 2026-06-03T01:06:36+00:00

I was solving a Python question on CodingBat.com. I wrote following code for a

  • 0

I was solving a Python question on CodingBat.com. I wrote following code for a simple problem of printing a string n times-

def string_times(str, n):
    return n * str

Official result is –

def string_times(str, n):
    result = ""
    for i in range(n):
       result = result + str
    return result

print string_times('hello',3)

The output is same for both the functions. I am curious how string multiplication (first function) perform against for loop (second function) on performance basis. I mean which one is faster and mostly used?

Also please suggest me a way to get the answer to this question myself (using time.clock() or something like that)

  • 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-03T01:06:37+00:00Added an answer on June 3, 2026 at 1:06 am

    We can use the timeit module to test this:

    python -m timeit "100*'string'"
    1000000 loops, best of 3: 0.222 usec per loop
    
    python -m timeit "''.join(['string' for _ in range(100)])"
    100000 loops, best of 3: 6.9 usec per loop
    
    python -m timeit "result = ''" "for i in range(100):" "  result = result + 'string'"
    100000 loops, best of 3: 13.1 usec per loop
    

    You can see that multiplying is the far faster option. You can take note that while the string concatenation version isn’t that bad in CPython, that may not be true in other versions of Python. You should always opt for string multiplication or str.join() for this reason – not only but speed, but for readability and conciseness.

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

Sidebar

Related Questions

I'm solving the coding problems on codingbat in Python. The make_bricks problem is defined
I am solving the problem in the interview question about LinkedList. question is... Write
Working on solving a simple problem in C++, want to understand how I could
Consider this way of solving the Subset sum problem: def subset_summing_to_zero (activities): subsets =
I am trying to call urllib2 simple code. import urllib2 f = urllib2.urlopen('http://www.python.org/') print
I'm looking for an efficient way of solving a particular problem in Python. I
I wrote a simple python program that looks to me like it should be
i was solving the problem https://www.spoj.pl/problems/ACPC11A/ and here is my code : #include<iostream> #include<cstdio>
After solving countless problems with the hg-fast-export tool on Windows (from finicky python version
I'm solving UVA's Edit Step Ladders on an uva sub-site named programming-challenges.com, but since

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.