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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:19:38+00:00 2026-05-27T08:19:38+00:00

Let’s assume we only have one line to work with. We have string fo0,

  • 0

Let’s assume we only have one line to work with. We have string “fo0”, and we want to print each character a random amount of times, and redo that entire statement another random amount of times; Resulting in something like the following:

ffooooo000 ffffffoo00 fffo00000000

How does one go about doing this, using for loops or something similar?

As far as I’ve gotten:

import random; x = "fo0"; print "".join(x for i in range(0,random.randrange(0,40)))

All this results in is something like “fo0fo0fo0fo0fo0fo0“.

  • 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-05-27T08:19:39+00:00Added an answer on May 27, 2026 at 8:19 am

    You can use something like the following. Below, s is the input string, char_rand_max is the maximum number of times a single character is repeated in a word, and str_rand_max is the maximum number of times to produce a word. Each character appears at least once, and there is at least one word.

    import random
    
    def rand_print(s, char_rand_max, str_rand_max):
        for i in range(random.randint(1, str_rand_max)):
            print ''.join([c*random.randint(1, char_rand_max) for c in s]),
        print
    

    Examples:

    >>> s = 'fo0'
    >>> rand_print(s, 5, 5)
    fffoooo0000
    
    >>> rand_print(s, 5, 5)
    fo000 fffffooo00000 ffooo00000 fffooo00 fffo0
    
    >>> rand_print(s, 5, 5)
    ffffo00 fffffooo00000 fffffo00000
    

    One-line solution

    You can, of course, do this all in one line, if you want. Rather than squashing the function from above into a single line, you can do the following:

    from random import randint as ri; x = 'fo0'; \
        print ' '.join([''.join([c*ri(1, 5) for c in x]) for i in range(ri(1, 5))])
    

    Note that I added a line break to make it more readable in the answer. Also, unlike the solution above, the maximum numbers of character repeats and words is hard-coded at five.

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

Sidebar

Related Questions

Let's say I have the string: hello world; some random text; foo; How could
Let's say I have a drive such as C:\ , and I want to
let's say I have the following string: string s = A B C D
Let's suppose I have this piece of code. foreach(string value in myList) { string
Let's assume I want to deserialize this (I've removed the namespaces to make things
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Let's say I have a C++ Visual Studio 2010 solution with 2 projects: one
Let's say R is a mxn random matrix. How can generate R with each
Let's say for example i have URL containing the following percent encoded character :
let's say that I have string: s = Tuple: and Tuple (stored in a

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.