Python3 string "abcd" print: aababcabcd?

If a has a string like abcdor 1234etc., how can I print together, the first character, then the first two characters, then the first three, etc. together?

For example, for string = 1234I would like to print / return 1121231234oraababcabcd

I have this code:

def string_splosion(str):
    i = 0
    while i <= len(str):
        i += 1
        print(str[:i])
print(string_splosion('abcd'))

But it prints / returns it on separate lines. I could write it manually like print(str[0:1], str[1:2] <...>), but how to make python do it, since I don't know how long the string will be?

+4
source share
3 answers

str , str . :

def string_splosion(string):
    i, result = 0, ''
    while i < len(string): # < instead of <=
        i += 1
        result += string[:i]
    return result

, str.join range:

def string_splosion(string):
    return ''.join(string[:i] for i in range(1, len(string) + 1))

itertools.accumulate (Python 3.2 +):

import itertools
def string_splosion(string):
    return ''.join(itertools.accumulate(string))

itertools.accumulate 2 , str.join 1,5 , :

string_splosion_loop(abcdef): 2.3944241080715223
string_splosion_join_gen(abcdef): 2.757582983268288
string_splosion_join_lc(abcdef): 2.2879220573578865
string_splosion_itertools(abcdef): 1.1873638161591886

, ,

import itertools
from timeit import timeit

string = 'abcdef'

def string_splosion_loop():
    i, result = 0, ''
    while i < len(string):
        i += 1
        result += string[:i]
    return result

def string_splosion_join_gen():
    return ''.join(string[:i] for i in range(1, len(string) + 1))

def string_splosion_join_lc():
    # str.join performs faster when the argument is a list
    return ''.join([string[:i] for i in range(1, len(string) + 1)])

def string_splosion_itertools():
    return ''.join(itertools.accumulate(string))

funcs = (string_splosion_loop, string_splosion_join_gen, 
         string_splosion_join_lc, string_splosion_itertools)

for f in funcs:
    print('{.__name__}({}): {}'.format(f, string, timeit(f)))
+6

:

"".join([s[:i] for i in range(len(s)+1)])

@abc, str , . . https://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange

:.

>>> s = "1234"
>>> "".join([s[:i] for i in range(len(s)+1)])
'1121231234'
>>> s = "abcd"
>>> "".join([s[:i] for i in range(len(s)+1)])
'aababcabcd'

range(len(s)+1) - , . Python:

>>> s = "1234"
>>> len(s)
4
>>> range(len(s))
[0, 1, 2, 3]
>>> s[:3]
'123'
>>> range(len(s)+1)
[0, 1, 2, 3, 4]
>>> s[:4]
'1234'

:

>>> s[:0]
''
>>> s[:1]
'1'
>>> s[:2]
'12'
>>> s[:3]
'123'
>>> s[:4]
'1234'

, list([s[:1], s[:2], s[:3], s[:4]]) "".join(list), . https://docs.python.org/2/library/string.html#string.join:

>>> list([s[:1], s[:2], s[:3], s[:4]])
['1', '12', '123', '1234']
>>> x = list([s[:1], s[:2], s[:3], s[:4]])
>>> "".join(x)
'1121231234'
>>> "-".join(x)
'1-12-123-1234'
>>> " ".join(x)
'1 12 123 1234'

, range(1,len(s)+1), s[:0] 0:

>>> s = "1234"
>>> "".join([s[:i] for i in range(1,len(s)+1)])
'1121231234'
>>> "".join([s[:i] for i in range(len(s)+1)])
'1121231234'
+4

python 3, :

print(yourString, end="")

, :

def string_splosion(str):
    for i in range(len(str)):
        print(str[:i], end="")
print(string_splosion('abcd'))
0

Source: https://habr.com/ru/post/1569214/


All Articles