How to combine individual characters in one string in python

This question already has an answer here:

  • How to print without newline or space? 26 answers

  • def reverse(text):
        if len(text) <= 1:
            return text
        return reverse(text[1:]) + text[0]
    print (reverse('abc!de@fg'))
    

    To print without the newline at the end of each line do:

    print('text', end='')
    

    To take a list of characters and make them one string, do:

    ''.join(list_of_characters)
    

    The simplest way to reverse a string:

    In [1]: a = "abc!de@fg"
    
    In [2]: print(a[::-1])
    gf@ed!cba
    

    The python print statement adds a newline by default. An easy way to print without newlines is

    sys.stdout.write('some text')
    # or
    print('some text', end='')
    
    链接地址: http://www.djcxy.com/p/77228.html

    上一篇: 如何停止一个额外的尾随换行符?

    下一篇: 如何在Python中将单个字符组合在一个字符串中