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.

Answered by Dominykas S. Python tutor

3722 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

Write a program that computes the sum of all numbers up to a input number n


How would you write a while loop to print all even numbers from 1-10?


Write a python function that takes a string as parameter and returns the character in the string with the most occurrences, along with the number of times this character occurs


Write a function that takes a string, and outputs that string formatted in camelcase. (alternating upper and lower case for each character, e.g. cAmElCaSe)


We're here to help

contact us iconContact usWhatsapp logoMessage us on Whatsapptelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2024

Terms & Conditions|Privacy Policy