Home>

I want to find a specific string and do a line break there. For example,
employee
I want to change the e to \ ne. That is,

  

e
  mploy
  e
  e

so we want to output"employee".replace ("e","\ ne")

  

\ nemploy \ ne \ ne

It will be

. What should I do?

By the way, finally, read the file where tuples are written at random like (1,3), (4,6), (7,53), (65,143)…
1
3
4
6
7
53
65
143

The goal is to make it output.

  • Answer # 1

    You just see the escape when it appears, but it should actually contain a newline character. You can see it by printing.

    >>>s = "employee" .replace ("e", "\ ne")
    >>>s
    '\ nemploy \ ne \ ne'
    >>>print (s)
    employer
    e
    e

  • Answer # 2

    First Half

    Split by a specific string

    ['&apos ;,'mploy&apos ;,'&apos ;,'']

    Return empty string tospecific string

    ['e&apos ;,'mploy&apos ;,'e&apos ;,'e']

    Concatenate with line break

    e
    mploy
    e
    e
    Click here for completion
    word = "employee"
    c = "e"
    print ("\ n" .join ([s if s else c for s in word.split (c)]))
    Second half game
    text = "(1,3), (4,6), (7,53), (65,143)"
    print (text.translate (str.maketrans ("(),", "\ 0 \ 0 \ n")))
    1
    Three
    Four
    6
    7
    53
    65
    143

  • Answer # 3

    If you want to replace a specific character in the first string and start a new line, you can do it below. It's not a very clean process.

    x = "employee"
    x = x.replace ("e", "\ ne \ n")
    x = x.strip (). replace ("\ n \ n", "\ n")


    You can also write: x is the target string, and target is the replacement string.
    Since f-strings is used, it doesn't work unless it is more than Python3.6.

    x = x.replace (target, f "\ n {target} \ n"). strip (). replace ("\ n \ n", "\ n" )

    Also, for the final purpose, you can output each element of the tuple with a new line by writing as follows. This assumes that you are loading a list of tuples.

    x = [(1, 2), (2, 3), (3, 4)]
    for i in x:
        for j in i:
            print (j)