Python Divide User In Put By 4 - Stack Overflow

    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs Python divide user in put by 4 Ask Question Asked 10 years, 11 months ago Modified 10 years, 11 months ago Viewed 162 times 0

How can I divide the user input by 4?

example = input("Enter input")

so abcdefghjklmnopqrstu is 20 characters and I want to divide them by 4 so Ill have 5 characters list then I will append them all into a newlist?

I am new to python and using Python IDLE 3.3

I would like to get the total of user's input character then divided them by 4 so when I want to append them to a list it would look something like this

list = [abcde],[ghijk],[lmnop],[qrstu]

Here is my current code

user = input('ENTER TEXT: ') user.upper() print('Decrypting message: ', user) for i in range(0, len(user), 4): temp.append(user[i:i+4]) for i in range(0, len(user), 1): print(temp[i]) Share Improve this question Follow edited Jan 12, 2014 at 18:49 DaveDave asked Jan 12, 2014 at 18:30 DaveDave's user avatar DaveDaveDaveDave 3031 gold badge2 silver badges11 bronze badges 2
  • possible duplicate of split string into groups of given size – Iguananaut Commented Jan 12, 2014 at 19:05
  • Really what you're asking is how to split a string into equal sized chunks. It's not really relevant that the string came from an input call. – Iguananaut Commented Jan 12, 2014 at 19:06
Add a comment |

2 Answers 2

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 7

You could use textwrap.wrap() for this:

>>> import textwrap >>> s = "abcdefghjklmnopqrstu" >>> textwrap.wrap(s, len(s)/4) ['abcde', 'fghjk', 'lmnop', 'qrstu'] Share Improve this answer Follow answered Jan 12, 2014 at 18:42 Chris Seymour's user avatar Chris SeymourChris Seymour 85.6k32 gold badges165 silver badges206 bronze badges 2
  • Needs to be "textwrap.wrap(s, len(s)//4)" for Python 3, which asker is using. "len(s)/4" gives floating point, which throws a TypeError when it tries to slice the string in the wrap() implementation. – CoupleWavyLines Commented Jan 12, 2014 at 19:18
  • @CoupleWavyLines good point. My OS still default to 2.7, nice catch. – Chris Seymour Commented Jan 12, 2014 at 23:43
Add a comment | 0

In case you don't want to use any library, can achieve it using loop:

a = "abcdefghijklmnopqrstu" l = len(a); i = 0; r = [] while i < l and len(a) >= 4: r.append(a[:l/4]) a = a[l/4:] i += l/4 print (r) ['abcde', 'fghij', 'klmno', 'pqrst'] Share Improve this answer Follow answered Jan 12, 2014 at 19:36 Shaifali's user avatar ShaifaliShaifali 3093 silver badges9 bronze badges 4
  • Python is not C and this has issue with edge cases: try with abcdefghijklmnopqrstuvwx – Chris Seymour Commented Jan 12, 2014 at 23:44
  • @sudo_O: Yes you are correct Python is not C; Python have full-fledged library support which is it's plus point; but I thought to give this solution for newbiees as the person who is asking question(Mr.T) has mentioned that he is new to python so I just wanted to let him know all ways to solve above problem; with or without library. – Shaifali Commented Jan 13, 2014 at 15:19
  • When I said Python is not C I was referring to style only... it's not very pythonic. – Chris Seymour Commented Jan 13, 2014 at 16:13
  • okay; but it is python program; any ways I will keep it in mind. – Shaifali Commented Jan 15, 2014 at 14:10
Add a comment |

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved Draft discarded

Sign up or log in

Sign up using Google Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.

  • The Overflow Blog
  • The real 10x developer makes their whole team better
  • The ghost jobs haunting your career search
  • Featured on Meta
  • The December 2024 Community Asks Sprint has been moved to March 2025 (and...
  • Stack Overflow Jobs is expanding to more countries

Linked

1 split string into groups of given size 2 How can I divide two integers stored in variables in Python? 0 How to divide numbers in python 1 How to divide a list in python? 4 Dividing the first element of a list by the second and the third by the fourth and so on 0 How to divide an integer by a list in Python? 2 Divide values in Python 1 How to divide two elements of list using python 0 Dividing multiple numbers in a list 3 Divide and distribute the result 0 How to divide a specific number in a list?

Hot Network Questions

  • Clarification on MPPT circuit
  • Where did Tolstoy write that a man is like a fraction?
  • Is mathematics just "a part of physics", as stated by Arnold in 1997?
  • How often do Bonus Picks occur?
  • If I have a multiannual stay-permit in France, will they still check biometrics on arrival at the airport?
  • Find the word pairs
  • What is type of probability is involved when mathematicians say, eg, "The Collatz conjecture is probably true"?
  • Why are the layers of the James Webb Telescope’s sunshield so thin?
  • I'm looking for a science fiction book about an alien world being observed through a lens. The world is inhabited by a race of lobster-like beings
  • Is Luke 4:8 enjoining to "worship and serve" or serve only
  • Visual aspect of an iron star
  • Debian Bookworm always sets `COLUMNS` to be a little less than the actual terminal width
  • How to limit width of a cell in an array?
  • Did the Israelites defecate when eating the manna?
  • Notation for Organ Registration in Bach/Árpád Kommt Ihr Töchter
  • Destruction of Books in Jihad
  • Conditional anchor positioning based on pgf keys
  • Name that logic gate!
  • Responsibility of scientific theories?
  • Why kinetically controlled enolate is not formed?
  • Extract signer information from portable executable (PE)
  • Can i travel to India if my passport expires in 25 March 2025?
  • Corporate space exploration/espionage
  • Is honey good or bad for kids?
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-py

Từ khóa » F G H J K L M N O P