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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:15:46+00:00 2026-06-15T16:15:46+00:00

Is it possible to split a string in python and assign each piece split

  • 0

Is it possible to split a string in python and assign each piece split off to a variable to be used later? I would like to be able to split by length if possible, but im not sure how it would work using len().

i tried this but its not getting me what i needed:

x = 'this is a string'
x.split(' ', 1)
print x

result:
[‘this’]

i want to result to something like this:

a = 'this'
b = 'is'
c = 'a'
d = 'string'
  • 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-15T16:15:48+00:00Added an answer on June 15, 2026 at 4:15 pm

    If you’d like to access a string 3 characters at a time, you’re going to need to use slicing.

    You can get a list of the 3-character long pieces of the string using a list comprehension like this:

    >>> x = 'this is a string'
    >>> step = 3
    >>> [x[i:i+step] for i in range(0, len(x), step)]
    ['thi', 's i', 's a', ' st', 'rin', 'g']
    >>> step = 5
    >>> [x[i:i+step] for i in range(0, len(x), step)]
    ['this ', 'is a ', 'strin', 'g']
    

    The important bit is:

    [x[i:i+step] for i in range(0, len(x), step)]
    

    range(0, len(x), step) gets us the indices of the start of each step-character slice. for i in will iterate over these indices. x[i:i+step] gets the slice of x that starts at the index i and is step characters long.

    If you know that you will get exactly four pieces every time, then you can do:

    a, b, c, d = [x[i:i+step] for i in range(0, len(x), step)]
    

    This will happen if 3 * step < len(x) <= 4 * step.

    If you don’t have exactly four pieces, then Python will give you a ValueError trying to unpack this list. Because of this, I would consider this technique very brittle, and would not use it.

    You can simply do

    x_pieces = [x[i:i+step] for i in range(0, len(x), step)]
    

    Now, where you used to access a, you can access x_pieces[0]. For b, you can use x_pieces[1] and so on. This allows you much more flexibility.

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

Sidebar

Related Questions

Possible Duplicate: Split String into smaller Strings by length variable I have seen solutions
Possible Duplicate: Split string on whitespace in python I have a string like so:
I would like to split a string according to the title in a single
We would like to split a string on instances of the pipe character |
Possible Duplicate: split a string in python I want to change this: str =
Possible Duplicates: Split python string every nth character? What is the most “pythonic” way
Possible Duplicate: Python: Split string with multiple delimiters I have a small syntax problem.
Possible Duplicate: Python: Split string with multiple delimiters Convert django Charfield \t to tab
Possible Duplicate: Python: Split string with multiple delimiters I have a program where I
Possible Duplicate: Splitting a string into a list in python I'd like to take

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.