Python Forum
Python Consecutive characters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Consecutive characters
#5
You can do this with regular expressions
import re
regex = re.compile(r'((.)\2*)')

matches = regex.finditer("aaaabbbccddddddddeee")
s = ""
for match in matches:
    if len(match.group(0)) > 3:
        s += str(len(match.group(0)))+"Z"+match.group(0)[0]
    else:
        s += match.group(0)

print(s)
Reply


Messages In This Thread
Python Consecutive characters - by vijju56 - Apr-21-2020, 04:59 PM
RE: Python Consecutive characters - by deanhystad - Apr-21-2020, 05:48 PM
RE: Python Consecutive characters - by bowlofred - Apr-21-2020, 08:06 PM
RE: Python Consecutive characters - by deanhystad - Apr-21-2020, 08:34 PM
RE: Python Consecutive characters - by TomToad - Apr-22-2020, 11:03 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Extract continuous numeric characters from a string in Python Robotguy 2 3,852 Jan-16-2021, 12:44 AM
Last Post: snippsat
  Python win32api keybd_event: How do I input a string of characters? JaneTan 3 5,297 Oct-19-2020, 04:16 AM
Last Post: deanhystad
  Remove escape characters / Unicode characters from string DreamingInsanity 5 22,647 May-15-2020, 01:37 PM
Last Post: snippsat
  Assign the sum of 2 consecutive numbers in a list to a varibale Fenaz 3 3,072 Apr-05-2020, 10:30 PM
Last Post: bowlofred
  creating consecutive rows PyPy 1 2,157 Feb-07-2020, 06:46 PM
Last Post: micseydel
  Slicing Python list of strings into individual characters Drone4four 5 5,474 Apr-17-2019, 07:22 AM
Last Post: perfringo
  problems with python script and special characters last08 1 3,026 Mar-29-2019, 09:28 AM
Last Post: Kebap
  modify the color a string of characters in python 3.6 atlass218 10 7,623 Feb-28-2019, 03:20 PM
Last Post: atlass218
  Replace characters from file in Python 2.7 melmouja 2 3,399 Feb-04-2019, 01:32 PM
Last Post: melmouja
  I need to add consecutive numbers help juniorcoder 1 3,073 Sep-18-2018, 01:01 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020