The main focus of this post is join() method.
join method takes all elements of list/tuple and joins them to string.
"-".join(my_list)
every element in list is joined by a colon and the resulting type is string.
"character to join the elements".join(my_list)
is the syntax of join method.
1)Read a given string, change the character at a given index and then print the modified string.
Input:
abracadabra
5 k
Output:
abrackdabra
2)Split a line and join the words using "-"
Sample Input
this is a string
Sample Output
this-is-a-string
split method splits the line into separate elements and result of line=line.split() is a list.
line=line.split(" ")
forms a list with every element in input separated by a space.
line=line.split(",")
forms a list with every element in input separated by a comma.
No comments:
Post a Comment