How can I write code that searches for each '\n' newline character in a string and then adds an asterisk after it? I don't want to use 'str.split()' [closed]
I would like to write a program that searches for each \n newline character in a string and then adds an asterisk just after that. For example, a string like this:
string= 'we will do it\nwe can do it\nwe want to do it\nwe have to do it'
The result should look like this:
*we will do it
*we can do it
*we want to do it
*we have to do it
I tried the code below which splits the string at where '\n' occurs into a list, then I used a for loop to add the asterisks:
string= 'we will do it\nwe can do it\nwe want to do it\nwe have to do it'
new_string = string.split('\n')
for i in new_string:
print("*"+i)
It worked perfectly but I would like to use another method which is: to write code that searches for each \n newline character in the string and then adds the star just after that. (for learning purposes)
source https://stackoverflow.com/questions/72930414/how-can-i-write-code-that-searches-for-each-n-newline-character-in-a-string-a
Comments
Post a Comment