5

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 
Andrew Li
  • 55,805
  • 14
  • 125
  • 143
Jitesh
  • 195
  • 2
  • 16
  • 5
    It is easier to simply `return text[::-1]` as your `reverse` function, unless this is a homework assignment or something – Cory Kramer Oct 09 '16 at 15:44
  • 3
    A function which is designed to `return` something typically wouldn't contain `print` statements. Return a string. Let the user of the function decide if they want to print the return value. – John Coleman Oct 09 '16 at 15:47

6 Answers6

3

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)
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
1

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='')
R. S. Nikhil Krishna
  • 3,962
  • 1
  • 13
  • 27
1
def reverse(text):
    if len(text) <= 1:
        return text
    return reverse(text[1:]) + text[0]
print (reverse('abc!de@fg'))
0

You will need to concatenate them and then print the string result.

This is because the function print automatically does a break line.

A fix would be:

def reverse(text):
       l = len(text)
       aux = ''

       while l-1 != 0:
           aux += str(text[l-1])
           l = l - 1

       print (aux)   
       a = reverse("abc!de@fg")`

P.S: Try to avoid redundant parenthesis =)

Wrong
  • 1,195
  • 2
  • 14
  • 38
0

Hey try to put the print value in variable and print that at the end. Like this.

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

        l=l-1
    output +=(str(text[0]))

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

Hope this helps

Harv
  • 446
  • 5
  • 12
0

In Python, the simplest way to reverse a Sequence is with a slice:

>>> string="abc!de@fg"
>>> print(string[::-1])
gf@ed!cba

Slicing will give you a copy of the sequence in reverse order.

In general, you want a function to do one thing. Returning a value and printing some output are separate concerns, so they should not be conflated in the same function. If you have a function that returns a reversed string, you can simply print the function:

print(reverse("abc"))

Then, you can see that your actual algorithm is fine, if not entirely Pythonic:

def reverse(text):
    l=len(text)
    output = ""
    while ((l-1)!=0):
        output += (str(text[l-1]))
        l=l-1
    output +=(str(text[0]))
    return output

You can clearly see now that it's the print function in your way.

kojiro
  • 74,557
  • 19
  • 143
  • 201