Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Split a String Between Numbers and Letters?

#1
How to Split a String Between Numbers and Letters?

Problem Formulation: Given a string of letters and numbers. How to split the string into substrings of either letters or numbers by using the boundary between a letter and a number and vice versa.

Examples: Have a look at the following examples of what you want to accomplish.

'111A222B333C' ---> ['111', 'A', '222', 'B', '333', 'C']
'Finxter42' ---> ['Finxter', '42']
'Hello world' ---> ['Hello', ' world']


Method 1: re.split()


The re.split(pattern, string) method matches all occurrences of the pattern in the string and divides the string along the matches resulting in a list of strings between the matches. For example, re.split('a', 'bbabbbab') results in the list of strings ['bb', 'bbb', 'b'].

# Method 1: re.split()
import re
s = '111A222B333C'
res = re.split('(\d+)', s)
print(res)
# ['', '111', 'A', '222', 'B', '333', ' C']

The \d special character matches any digit between 0 and 9. By using the maximal number of digits as a delimiter, you split along the digit-word boundary. Note that you don’t consume the split character by wrapping it into a matching group using the parentheses (...). If you leave out the parentheses, it’ll consume the numbers and the result wouldn’t contain any consecutive numbers.

Method 2: re.findall()


The re.findall(pattern, string) method scans string from left to right, searching for all non-overlapping matches of the pattern. It returns a list of strings in the matching order when scanning the string from left to right.

# Method 2: re.findall()
import re
s = '111A222B333C' res = re.findall('(\d+|[A-Za-z]+)', s) print(res)
# ['111', 'A', '222', 'B', '333', 'C']

Method 3: itertools.groupby()


# Method 3: itertools.groupby()
from itertools import groupby
s = '111A222B333C' res = [''.join(g) for _, g in groupby(s, str.isalpha)]
print(res)
# ['111', 'A', '222', 'B', '333', 'C']
  • The itertools.groupby(iterable, key=None) function creates an iterator that returns tuples (key, group-iterator) grouped by each value of key. We use the str.isalpha() function as key function.
  • The str.isalpha() function returns True if the string consists only of alphabetic characters.

Related Video re.split()




Where to Go From Here?


Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

The post How to Split a String Between Numbers and Letters? first appeared on Finxter.



https://www.sickgaming.net/blog/2021/01/...d-letters/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How to Remove Everything After the Last Character in a String? xSicKxBot 0 23 02-02-2021, 08:23 AM
Last Post: xSicKxBot
  [Tut] How To Cut A String In Python? xSicKxBot 0 28 02-01-2021, 04:09 PM
Last Post: xSicKxBot
  [Tut] Python Int to String with Leading Zeros xSicKxBot 0 55 11-17-2020, 03:22 AM
Last Post: xSicKxBot
  [Tut] How to Get MD5 of a String? A Python One-Liner xSicKxBot 0 75 11-07-2020, 03:58 AM
Last Post: xSicKxBot
  [Tut] How To Format A String That Contains Curly Braces In Python? xSicKxBot 0 85 09-25-2020, 07:51 PM
Last Post: xSicKxBot
  [Tut] How to Access an Object Attribute Given the Attribute Name as a String? xSicKxBot 0 89 09-04-2020, 08:14 PM
Last Post: xSicKxBot
  [Tut] Create a List of Random Numbers — The Most Pythonic Way xSicKxBot 0 165 07-07-2020, 04:23 PM
Last Post: xSicKxBot
  [Tut] The Most Pythonic Way to Convert a List to a String xSicKxBot 0 130 06-21-2020, 01:35 PM
Last Post: xSicKxBot
  [Tut] string.join(list) vs list.join(string) | Why Python’s Creators Chose The Former xSicKxBot 0 175 06-19-2020, 05:12 AM
Last Post: xSicKxBot
  [Tut] The Most Pythonic Way to Convert a List of Tuples to a String xSicKxBot 0 128 06-09-2020, 12:22 AM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
Blender 2.92 Released
Last Post: xSicKxBot
Today 01:09 PM
» Replies: 0
» Views: 4
Fedora - Contribute at the Fedora Audio,...
Last Post: xSicKxBot
Today 01:09 PM
» Replies: 0
» Views: 2
News - George Romero's Lost Movie The Am...
Last Post: xSicKxBot
Today 01:09 PM
» Replies: 0
» Views: 9
[Tut] How to Read a File Without Newline...
Last Post: xSicKxBot
Today 06:41 AM
» Replies: 0
» Views: 8
(Indie Deal) FREE Syberia 2 | Risk of Ra...
Last Post: xSicKxBot
Today 06:41 AM
» Replies: 0
» Views: 9
(Free Game Key) Black Desert Online - Fr...
Last Post: xSicKxBot
Today 06:41 AM
» Replies: 0
» Views: 9
News - Splatoon 2 Version 5.4.0 Is Now L...
Last Post: xSicKxBot
Today 03:09 AM
» Replies: 0
» Views: 8
News - Nightdive Studios Adds Online Mul...
Last Post: xSicKxBot
Today 03:09 AM
» Replies: 0
» Views: 10
News - South Park Releases Vaccination S...
Last Post: xSicKxBot
Today 03:09 AM
» Replies: 0
» Views: 27
Xbox Wire - This Week on Xbox: February ...
Last Post: xSicKxBot
Today 12:04 AM
» Replies: 0
» Views: 9

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020