Creating string from multiple strings(or characters)

In Python it is possible to add two strings or characters by using + sign string_a = "" for i in range(10): string_a += str(i) Now string_a would have "0123456789" value. If you add only two strings, this way is completely fine. The problem is when you add more than two strings. It still would work but it wouldn’t be very efficient. In Python, if you use + sign to add strings, it destroys original string and creates new one. In this example it happened 10 times. There is a better way to do it. We have to use join class to join list elements list_a = [] for i in range(10): list_a.append(str(i)) string_b = "".join(list_a) It creates the same result, just way more efficiently.

DS

Related Python Mentoring answers

All answers ▸

Write a function that checks whether a number is prime or not.


Write a recursive function to find the factorial of a number n.


[Exam style] Python is an interpreted language. Explain what this means and how interpreted languages differ from compiled languages.


Write a simple number guessing game, give the user 3 tries to guess a number between 1 and 10