After that, each element of the list is traversed using a for loop, and for every index, the element would be added to the initialized string. Then an empty string has to be initialized to store the elements. In this example, firstly we declare a list that has to be converted to a string. Refer to the example given below to understand clearly. For a case like this, str() function will first be used to convert the other data type into a string and then further, the join function will be applied. In this case, The join function can not be used directly. Now, there may be a case when a list will contain elements of data type other than string. Note that each element in the new string is delimited with a single space. Here, all the elements in the list are individual string, so we can use the join function directly. The main point to keep in mind while using this function is that the join function can convert only those lists into string that contains only string as its elements. The join function is one of the simplest methods to convert a list to a string in python. How to Convert a List to String in Python The indexing of elements in a string also starts from 0. An element of a string can be accessed in the same way as we saw in the case of a list. Here, a is a variable assigned to the string simplilearn. Here is an example of string in python and how to print it.įor declaring a string, we assign a variable to the string. This means that a string is a subset of sequence data type, containing all elements as characters. This is the main difference between the two.Ī sequence is a data type composed of multiple elements of the same data type, such as integer, float, character, etc. The point to be noted here is that a list is an ordered sequence of object types and a string is an ordered sequence of characters. A string in python is an ordered sequence of characters.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |