Igor
Igor

Reputation: 6245

How to make loop in python

ALL,

I have a following list:

["abc", "def", 'ghi"]

What I need to get from it is a string:

"abc,def,ghi"

There is a comma between first and second element, but not at the end of the string.

Unfortunately, Python does not have a construction like:

std::string str;
for( int i = 0; i < 3; i++ )
{
    str += list[i];
    if( i != 2 )
        str += ",";
}

How do I do it in Python?

Upvotes: 0

Views: 137

Answers (1)

Blender
Blender

Reputation: 298076

Think of it in terms of a goal, not the way you'd do it in C++. You want to join the elements together:

','.join(elements)

Upvotes: 7

Related Questions