How can I split a string in Python? [duplicate]

2024/11/16 10:40:25

Possible Duplicates:
Split python string every nth character?
What is the most “pythonic” way to iterate over a list in chunks?

I need to split a string into equal parts. For example, if I have this string:

string = "123456781234567812345678"

I need to cut every 8 characters:

["12345678", "12345678", "12345678"]
Answer

This is actually a great application for a python list comprehension one-liner!

my_str="123456781234567812345678"
splits=[my_str[x:x+8] for x in range(0,len(my_str),8)]
//splits = ["12345678","12345678","12345678"]

Let me know if you have any questions.

https://en.xdnf.cn/q/119204.html

Related Q&A

How to enable media device access in Edge browser using Selenium?

I have a Selenium test I want to run in Edge against a page which uses the webcam and microphone. In order for those media devices to work in the browser, the user has to allow the site access to the d…

Print specific rows that are common between two dataframes

i have a dataframe (df1) like this id link 1 google.com 2 yahoo.com 3 gmail.comi have another dataframe(df2) like this: id link numberOfemployees 1 linkedin.com …

What is the problem with buttons connection in PyQt5?

I have the problem that I cannot connect the implementation between two buttons. After I pressed the "Grayscale" button and got the grayscale image, I pressed the "Canny" button but…

Install ipykernel in vscode - ipynb (Jupyter)

Greetings! I am not able to install ipykernel and receiving following error. Please advise. print(Hello) Running cells with Python 3.10.5 64-bit requires ipykernel package. Run the following command to…

How do I write a form in django

I am writing a forums app for my final project in class and I am trying to write a form for creating a new thread, Basically all I need it to do is username: text box hereName of thread: Text box here…

Whats the difference between namespaces and names?

Im assuming the namespace is the allotted place in memory in which the name is to be stored. Or are they the same thing?

Finding a string in python and writing it in another file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 10 years ago.Improv…

Python error TypeError: function takes exactly 1 argument (5 given)

Traceback (most recent call last):File "wdd.py", line 164, in <module>file.write("temperature is ", temperature, "wet is ", humidity, "%\n") TypeError: fun…

Django session not available on two seperate requests

Description: In the django session docs it says:You can read it and write to request.session at any point in your view.But I cant access the session when making a second request to the same view: views…

Counting how many times there are blank lists in a list of list [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…