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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:46:11+00:00 2026-06-01T03:46:11+00:00

Possible Duplicate: The Python yield keyword explained Okay, I’ve probably phrased the question badly

  • 0

Possible Duplicate:
The Python yield keyword explained

Okay, I’ve probably phrased the question badly but this is the situation I have.

I have this line of code in Python 2.7 which I’m trying to understand:

yield (padding_zeros + number_string).encode("ascii")

In this line of code, padding_zeros is a string of a variable number of ‘0’s and number_string is a number in the form of a string which can be any number between 0 to, say 10000.

I’m pretty confident that the .encode("ascii") just converts the output of yield to ascii.

What I’m completely at sea about is what the yield (padding_zeros + number_string) does.

I know it initiates a generator but I’ve spent a lot of time searching online and reading up on the syntax but I still can’t work out what the generator actually does. It doesn’t help that this is my first time looking at python (my ultimate aim is to convert this code to C#).

So, basically, please could someone explain to me what this line of code does? Does it just add the two strings together or does it do something a bit more complicated?

For further context, this is the block that that line of code appears in:

for current_length in range(4, max_length + 1):
    for i in range(0, pow(10, current_length)):
        number_string = str(i)
        padding_zeros = "0" * (current_length - len(number_string))
        yield (padding_zeros + number_string).encode("ascii")

(max_length being exactly what it sounds like – a number indicating the maximum length of something)

Thanks in advance for any and all answers (even if they’re telling me not to be such a fricking noob) 🙂

EDIT: Thanks very much for the answers – even though I could only pick one as the best answe they were all very helpful. And thanks for the comments as well – as some of them pointed out, What does the "yield" keyword do in Python? is a very good general guide to yield, generators and iterations even if I didn’t find it an answer to my specific situation 🙂

  • 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-01T03:46:12+00:00Added an answer on June 1, 2026 at 3:46 am

    OK, you know about generators, so the yield part needs no explanation. Fine.

    So what does that line actually do? Not very much:

    It concatenates padding_zeros and number_string and then encodes the result to ASCII. Which in Python 2.7 is a no-op because the string is ASCII to begin with (it only consists of ASCII digits, by definition).

    In Python 3, it would be different; here the .encode() would have converted the string to a bytes object. But in Python 2, it doesn’t make any sense.

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

Sidebar

Related Questions

Possible Duplicate: Python conditional assignment operator Apologies for such a simple question, but googling
Possible Duplicate: Learn Python the Hard Way Exercise 17 Extra Question(S) In this exercise
Possible Duplicate: Call Python From PHP And Get Return Code probably a very stupid
Possible Duplicate: Python list confusion I've got one little question about Python lists: Why
Possible Duplicate: Python: Adding element to list while iterating This doesn't seem to work,
Possible Duplicate: Python or Ruby Interpreter on iOS I just discovered this apps pypad
Possible Duplicate: Python list append behavior Why does this code: x = [[]]*3 x[0].append('a')
Possible Duplicate: Python: Behaviour of increment and decrement operators Hi, I've tried this. ++num
Possible Duplicate: Python ‘==’ vs ‘is’ comparing strings, ‘is’ fails sometimes, why? Is a
Possible Duplicate: Python ‘==’ vs ‘is’ comparing strings, ‘is’ fails sometimes, why? In Python,

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.