Search code examples
pythonarraysregexstringsplit

Python split() function :: Need to split "int_32\n' " so that I get int_32 alone


Need to split "int_32\n' " so that I get int_32 alone.

I tried

x = "int_32\n' "
x.split("\n")

I also tried

x = "int_32\n' "
x.splitlines()

Both do not yield the required output which is int_32

Instead it yields int_32\n'

\n is what is creating an issue. Anyway I can do this?


Solution

  • use strip() to remove leading and trailing whitespace including \n, spaces and single quote '.

    x = "int_32\n' "
    new_x = x.strip("\n' ")
    print(new_x)