Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

New line every time X letter is met

So if I want to make a new line every time the letter A is ran into in a line, what would the proper code be for that? I can't figure it out with regex.
[Image: 3.png]
Not sure what you're asking for here..
The regex to find the letter a, upper- and lowercase is [Aa].
@Divined

Code
s = "uuuuuuuuuuuuuuAuuuuuuuuuAuuuuuAu"
new_s = "\nA".join(s.split("A"))
print(new_s)

the output is:
Code
uuuuuuuuuuuuuu
Auuuuuuuuu
Auuuuu
Au
string.split("A") returns a list of strings separated by the given piece of text ("A" in this case)
string.join(list) joins list items by placing the string between them

Actually this way looks better to me, it uses regex pattern (r"A") instead which makes it easier to search for specific types of characters within the text, and it's just one function working on the string instead of splitting into a list and joining it again.

Code
import re
s = "uuuuuuuuuuuuuuAuuuuuuuuuAuuuuuAu"
new_s = re.sub(r"A", "\nA", s)
print(new_s)


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by ChukozyOct 2, 2020 11:55 AM 4 213 Oct 2, 2020 11:55 AM
Last Post: G.P.D.
[C#] by Formula 1Dec 6, 2019 01:11 AM 23 699 Dec 6, 2019 01:11 AM
Last Post: itcorp
[Shell] by friiz1337Oct 18, 2019 04:45 AM 2 223 Oct 18, 2019 04:45 AM
Last Post: friiz1337