python simple loop_.join function

python join
join lists python
w3 schools python join
python join vs +
python append to string in loop
python join two strings with comma
join list python w3schools
python concatenate strings

I'm an amateur learner and would like to have more ideas on these.

This is what I want,

paper_doll('Hello') --> 'HHHeeellllllooo'

Here is my code and it doesn't work, but I have no ideas why.

def paper_doll(text):
for i in range(0,len(text)-1):
    return ''.join(text[i]*3)
paper_doll('Hello')

The result became 'HHH'.

Understood the following would work, def paper_doll(text): result = '' for char in text: result += char * 3 return result

But why .join doesn't work in this case?

def paper_doll(text):
    ret=[]
    for i in text:
       ret.append(i*3)
    return ''.join(ret) 

Should work. This returns each 3 letter iteration, joined together.

Python String join, Here shows, how to use Python string Join to concat strings in List, Dictionary etc. It is a simple example to show you the join function. Here, we This time, within the Python join function, we used the For Loop to iterate each item in a List. The join () method is a string method and returns a string in which the elements of sequence have been joined by str separator. string_name .join (iterable) string_name: It is the name of string in which joined elements of iterable will be stored. Parameters: The join () method takes iterable – objects capable of returning its members one

First, your code does not work because the return statement exits from the function on the first iteration loop, so it triples only the first letter, and that's all:

def paper_doll(text):
    for i in range(0,len(text)-1): # on 1st iteration: i = 0
        return ''.join(text[i]*3)  # on 1st iteration: text[i] equals 'H' ==> 'HHH' is returned

Secondly, here is a solution using comprehension, which is well adapted in your case to iterate over each character of a string:

def paper_doll(text):
    return ''.join(i*3 for i in text)

print(paper_doll('Hello'))   # HHHeeellllllooo

Python String join() method, The join() method provides a flexible way to create strings from iterable objects. It joins each element of an iterable (such as list, string and tuple) by a string  Definition and Usage. The join() method takes all items in an iterable and joins them into one string. A string must be specified as the separator.

Your initial problem was the return in your iteration. This short circuits the rest of the loop... as noted in other answers.

python can iterate through a string for you. Another answer using list comprehension:

def paper_doll(text):
   return ''.join([char*3 for char in text])

ConcatenationTestCode, On the win32 Python 2.4 I'm seeing the join sample above complete in less than half I tend to create a stream of strings in a loop that should be concatenated. Python string method join() returns a string in which the string elements of sequence have been joined by str separator. This method returns a string, which is the concatenation of the strings in the sequence seq. The separator between elements is the string providing this method

Add to a string during the loop, return the result:

def paper_doll(text):
    s = ''
    for i in range(0,len(text)):
        s += ''.join(text[i]*3)
    return s

print(paper_doll('Hello'))

Output:

HHHeeellllllooo

(I also removed the -1 in range so you get three "o"s)

4. More Control Flow Tools, To iterate over the indices of a sequence, you can combine range() and len() as Loop statements may have an else clause; it is executed when the loop It is simple to write a function that returns a list of the numbers of the  Python supports to have an else statement associated with a loop statement. If the else statement is used with a for loop, the else statement is executed when the loop has exhausted iterating the list. If the else statement is used with a while loop, the else statement is executed when the condition becomes false.

1.13. Loops and Sequences, Execution of the program ends with another simple loop. As with the indented block in a function, it is important to get the indentation right. Alter the code above​, so  The range() Function To loop through a set of code a specified number of times, we can use the range() function, The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.

Python Add Lists / Join / Concatenate Two or More Lists, It describes various ways to join/concatenate/add lists in Python. However, the one, itertools.chain() is a method defined in the itertools module. Python Add lists example # Sample code to add two lists using for loop # Test input lists  First, in Python, if your code is CPU-bound, multithreading won't help, because only one thread can hold the Global Interpreter Lock, and therefore run Python code, at a time. So, you need to use processes, not threads. This is not true if your operation "takes forever to return" because it's IO-bound—that is, waiting on the network or disk

Python String join() Method, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  Functions in Python A function is a set of statements that take inputs, do some specific computation and produces output. The idea is to put some commonly or repeatedly done task together and make a function, so that instead of writing the same code again and again for different inputs, we can call the function.

Comments
  • It works, but you leave the loop with return after the first iteration.
  • Or a generator expression: return ''.join(char*3 for char in text)
  • @FredLarson It's slightly more efficient to pass a list to join, due to how the iterator protocol works. (join has to scan the entire input before it can allocate a string object big enough to hold the result, and a list comprehension is faster than join having to consume an arbitrary iterable).
  • @chepner: News to me. Thanks!
  • thanks. But is there any reason behind? guess the main issue is +=, in which accumulate the result from text[0], text[1], text[2] and so on? is that true?
  • There's no reason to use += and join; ''.join(x) is equivalent to x if x is a string.
  • Yes, thats true. Btw I got a downvote probably because thats not the way you should do it. However it might help you understand the problem.
  • I downvoted because it's grossly inefficient and is a terrible example of how to write Python code, not because it failed to reach some Pythonic ideal.
  • @chepner the poster said "I have no ideas why". Now he has an idea why. that was the objective, not to write efficient code.