I want to convert one string to another in the list that stores the string.

Asked 2 years ago, Updated 2 years ago, 56 views

my_list = ['\nblue\nyellow\nred, \ngreen\nblack, \npink']

I want to convert \n part to @ character like the result screen below.

my_list = ['@blue@yellow@red, @green@black, @pink']

python list

2022-09-22 18:22

1 Answers

In Java, there is a replace method that switches certain characters, so I looked for it in Python.

https://www.geeksforgeeks.org/python-string-replace/

I think you can use the replace method on each string while looking up the list.

// java grammar.
List<String> new_list = new ArrayList<>();
for(String str : my_list) {
    new_list.add(str.replace("\n", "@"));
}

If you don't understand, I'll read the Python repeat and write it again~

// Python grammar.
for i in range(len(my_list)):
    my_list[i] = my_list[i].replace("\n", "@")

print my_list

I didn't know Python well, so I hurriedly watched it and made it into a code executor!


2022-09-22 18:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.