Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine individual characters in one string in python [duplicate]

Tags:

python

I have code like this:

def reverse(text):
    l=len(text)
    while ((l-1)!=0):
        print (str(text[l-1]))

        l=l-1
    print (str(text[0]))   

a=reverse("abc!de@fg")

The output is:

g
f
@
e
d
!
c
b
a

but I want to combine these individual characters and want out put like this:

gf@ed!cba 
like image 605
Jitesh Avatar asked Oct 09 '16 15:10

Jitesh


2 Answers

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)
like image 66
Patrick Haugh Avatar answered Oct 25 '22 03:10

Patrick Haugh


def reverse(text):
    if len(text) <= 1:
        return text
    return reverse(text[1:]) + text[0]
print (reverse('abc!de@fg'))
like image 20
Amitkumar Satpute Avatar answered Oct 25 '22 03:10

Amitkumar Satpute