question string manipulation | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

question string manipulation

output of my code is: extr @ ate @ rre @ str @ ial @ but it should be ext @ rat @ err … and so on How can i do that? https://code.sololearn.com/cqqAbZz6efUs/?ref=app

27th Jun 2023, 2:46 PM
Angela
Angela - avatar
5 Answers
+ 5
Per Bratthammar Thank you for these information brother! I love learning these.
27th Jun 2023, 6:22 PM
Dragon RB
Dragon RB - avatar
+ 4
Hi, Dragon RB ! Slicing is good to know. The basic is easy. Suppose: >>> s = "abcdefghij" Then s[i: j: k] creates a copy of that part of s that starts with index i and end with index j-1. k is the step length, and can be ignored if k=1. i, j are non negativ integers, if couning the index from left to rigth (otherwise the opposit) k is a non zero integer: >>> print(s[1:8:2]) bdfh print() has two keyword arguments: sep and end. As default end='\n ', that is the new line character. When you change end, first everything in the print function is printed, and after that it will end the output with what you assigned end to: >>> print(s, end='hello world\n') abcdefghijhello world if s is as above. And sep is a separator and separats what every you assign sep to, after every coma in print the print function: >>> print(1, 2, 3, 4, 5, sep='\n') 1 2 3 4 5 You can compare using sep keyword argument in print with the string method .join(): >>> print('\n'.join('12345')) 1 2 3 4 5
27th Jun 2023, 6:01 PM
Per Bratthammar
Per Bratthammar - avatar
+ 2
Here is the modified code: https://code.sololearn.com/cH3Q8aoHibXv/?ref=app Always remember that index always start at 0, so I added the index by 1 (in the if condition) to get the expected output. I also declared a string variable(for string builder) outside the for loop, because it is a good practice for that.
27th Jun 2023, 3:25 PM
Dragon RB
Dragon RB - avatar
+ 2
# Hi, Angela ! # Here’s another one: msg, step = "extraterrestrial", 3 for k in range(0, len(msg), step): print(msg[k: k+step], end=' @ ')
27th Jun 2023, 5:20 PM
Per Bratthammar
Per Bratthammar - avatar
+ 2
Per Bratthammar I think index slicing and end = "..." is a little difficult for me to understand.. Edit: nevermind, I got it!
27th Jun 2023, 5:29 PM
Dragon RB
Dragon RB - avatar