Splitting Strings into Numbers (Python)

How to split strings into text and number?

I would approach this by using re.match in the following way:

import re
match = re.match(r"([a-z]+)([0-9]+)", 'foofo21', re.I)
if match:
items = match.groups()
print(items)
>> ("foofo", "21")

How to split a string into numbers and characters

A regex find all approach might be appropriate here. We can find groups of all non digit or all digit characters, alternatively.

string = 'Hello, welcome to my world001'
parts = re.findall(r'\D+|\d+', string)
print(parts) # ['Hello, welcome to my world', '001']

Any way to split strings in Python at the place were an integer appears?

What about using regex? i.e., the re package in python, combined with the split method? Something like this could work:

import re
string = 'string01string02string23string4string500string'

strlist = re.split('(\d+)', string)
print(strlist)
['string', '01', 'string', '02', 'string', '23', 'string', '4', 'string', '500', 'string']

You would then need to combine every other element in the list in your case i think, so something like this:

cmb = [i+j for i,j in zip(strlist[::2], strlist[1::2])]
print(cmb)

['string01', 'string02', 'string23', 'string4', 'string500']

Splitting strings of numbers in python

When you put in the number 0123, it is put in as an integer. Integers have no leading zeros built in, so it reduces itself to 123 before it's converted to the string by the str() function.

So your program is seeing str(123) instead of str(0123).

You can fix this by making the number a string by use of quotes instead of str():

for x in "0123":
print x

Edit: Based on a comment, I realized my explanation had an error. Although the stuff about the integers having no leading zeros built in, there's actually a behavior in Python 2 that does something different.

Integers written with leading zeroes are octal numbers in python. So when you put 0123, str() isn't seeing 123 like I said, it's actually seeing 1*8^2+2*8+3 = 83, and converts str(83), which is the output you are getting.

Regardless, closing the number in quotes still gets you what you want, just for a different reason than I was thinking.

How to split a string into a string and an integer?

You could split based on the space in the example you gave, unless you need a more generic approach.


# set up your variable
my_var = "variable 1"

# You can split by the space in this instance, if all of your data will look the same
my_split = my_var.split(" ")

# prints variable
print(my_split[0])

# prints 1
print(my_split[1])

how to split string and convert to integer

numbers = "51-52"
part = [int(x) for x in numbers.split("-")]
print(part)

convert string of numbers into list of integers in python

You can use map:

list(map(int, '12345678'))  # [1, 2, 3, 4, 5, 6, 7, 8]

Or a list comprehension:

[int(x) for x in '12345678']  # [1, 2, 3, 4, 5, 6, 7, 8]

split space separated numeric string into a list in Python3

Try this

.split() method returns a list of splitted strings. So you can iterate over it and convert it to a integer

A = '5 2 12 4 29'
B = [int(l) for l in A.split()]
['5', '2', '12', '4', '29']

.split() method will return something like this. But you want them in integers. So you can follow the above method

Split string into letters and numbers

import re

pathD = "M30,50.1c0,0,25,100,42,75s10.3-63.2,36.1-44.5s33.5,48.9,33.5,48.9l24.5-26.3"

print(re.findall(r'[A-Za-z]|-?\d+\.\d+|\d+',pathD))

['M', '30', '50.1', 'c', '0', '0', '25', '100', '42', '75', 's', '10.3', '-63.2', '36.1', '-44.5', 's', '33.5', '48.9', '33.5', '48.9', 'l', '24.5', '-26.3']


Related Topics



Leave a reply



Submit