Warning: session_start(): open(/tmp/sess_33e4578c59ac3f34f1184371e5da6afb, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Split A String Into Text And Number In Python - LearnShareIT

How To Split A String Into Text And Number In Python

Split a string into text and number in Python

Split a string into Text and Number in Python, that’s the topic I want to introduce to you today. I will show you the functions in the re module that can do this. Hope it helps you.

Split a string into text and number in Python

Use the re.findall function

The re.findall function is excellent for separating text and numbers from a string.

Syntax:

re.findall(regex, string))

Parameters:

  • regex: regular expression to search for digits.
  • string: string you want the regular expression to search for.

The findall() function returns a list containing the pattern matches in the string. If not found, the function returns an empty list.

Example:

  • Import the re module.
  • String initialization.
  • Use re.findall() split a string into text and number.
import re

myString = '123learnshareit34website'

# Use the re.findall() 
result = re.findall('(\d+|[A-Za-z]+)', myString)

print('Split string into text and numbers:', result) 

Output:

Split string into text and numbers: ['123', 'learnshareit', '34', 'website']

Use the split() function

Continue to use a re module function, the re.split() function. You can also use it to separate text and numbers from strings.

Syntax:

re.split(RegEx, string, maxsplit)

Parameters:

  • RegEx: are regular expressions.
  • string: string you want to compare.
  • maxsplit: is the maximum number of splits. If not specified, Python defaults to an infinite number of splits.

Example:

  • Import the re module.
  • String initialization.
  • Use the re.split() function split string into text and numbers.
import re

myString = '123learnshareit34website'

# Use the re.split() function
result = re.split('(\d+)', myString)

print('Split string into text and numbers:', result) 

Output:

Split string into text and numbers: ['', '123', 'learnshareit', '34', 'website']

Use the groupby() function.

You can combine the three functions in the re library to separate text and numbers from a string.

Syntax:

itertools.groupby(iterable, key=None)

Example:

  • Import the itertools module. 
  • Initialize a string.
  • The groupby() function creates an iterator that returns the corresponding tuple (key, group-iterator) by key. The tuple will be handled by the str.isalpha() function.
  • The isalpha() function returns true if the string has at least 1 character and all characters are letters. Otherwise the method will return false.
from itertools import groupby

myString = '123learnshareit34website'

# Use the itertools.groupby() function
result = [''.join(i) for _, i in groupby(myString, str.isalpha)]

print('Split string into text and numbers:', result)

Output:

Split string into text and numbers: ['123', 'learnshareit', '34', 'website']

Summary

Here are a few Python ways to split a string into text and number in Python. I think you should use the re.findall method. To read more articles, please visit our website to continue. Thanks for reading.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *