1

What I was trying to do:

Take a string and append a backwards copy of that string, making a palindrome

What I came up with:

# take an input string
a = input('Please enter a string: ')
a = list(a)

# read the string backwards
b = list(reversed(a))

# append the backward-ordered string to the original string, and print this new string
c = a + b
c = str(c)

print(c)

Question: When given a run, this script takes a string, for example "test", and returns ['t', 'e', 's', 't', 't', 's', 'e', 't']; I'm confused about this result since I explicitly converted c, as a result of concatenation of a and b, to a string. (c = str(c)) I know I must have missed some basic stuff here, but I wasn't able to figure out what. Could someone throw some light on this? Thank you!

And would anyone care to elaborate on why my c = str(c) didn't work? Thanks!

5
  • 1
    Just turn the list c back into a string like this: c = "".join(a+b) instead of c=str(c). Commented Jun 12, 2013 at 23:51
  • 1
    Im pretty sure it's a string but the commas and brackets are with the string Commented Jun 12, 2013 at 23:52
  • You don't actually need a = list(a) here. A string is already a sequence of characters, and you can already call reversed on it. Commented Jun 13, 2013 at 1:04
  • @abarnert thanks for pointing that out! I must have missed that. But on a related note, if a is set to be a string, would one be able to concatenate it with b? (i'm thinking about b=list(reversed(a))here), or is there another way (like b=reversed(a)? can you do that?) Commented Jun 13, 2013 at 1:29
  • If you want to use b=reversed(a) instead of b=a[::-1], then you will have to join up b, just as in nrpeterson's answer. Compactly: a + ''.join(reversed(a)). You may find that more readable than a + a[::-1], even though it requires some extra converting—if so, always go for the more readable version. Commented Jun 13, 2013 at 1:36

4 Answers 4

5

The problem with saying c = str(c) is that applying str to a list simply gives a string representation of that list - so, for instance, str([1,2,3]) yields the string '[1, 2, 3]'.

The easiest way to make a list of strings in to a string is to use the str.join() method. Given a string s and a list a of strings, running s.join(a) returns a string formed by joining the elements of a, using s as the glue.

For instance:

a = ['h','e','l','l','o']
print( ''.join(a) ) # Prints: hello

Or:

a = ['Hello', 'and', 'welcome']
print( ' '.join(a) ) # Prints: Hello and welcome

Finally:

a = ['555','414','2799']
print( '-'.join(a) ) # Prints: 555-414-2799
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the answer! and could you tell me why my c = str(c) didn't work? Thanks!
str(c) actually gives a string representation of the list, rather than forming a string out of the elements of the list. (So, it includes the brackets and commas!)
but at the beginning of my code, a =list(a) worked. Is there a difference between a=list(a) and c=str(c) ? (other than the fact that the former converts a to a list and the latter intends to convert c to string?)
Absolutely. str is a type conversion function - the idea is to come up with a string that represents the passed argument as faithfully as possible. So, if you pass it a list, it gives you what it thinks is a good string representation of a list - namely, with brackets to separate it from its surroundings, and commas to separate elements. The list function, on the other hand, is set up to take any object that acts like an iterator, and return a list of its contents.
Well, Python isn't actually making a decision of course; when you call str(object), python translates it in to a call to the method object.__str__(); whoever wrote the class that is the type of object determined what they thought the best string representation was. Since a list of numbers, a list of strings, and a list of various types of objects all just have the same type list, the designers of python didn't want to make too many assumptions.
|
1

It's worth understanding how to use join—and nrpeterson's answer does a great job explaining that.

But it's also worth knowing how not to create problems for yourself to solve.

Ask yourself why you've called a = list(a). You're trying to convert a string to a sequence of characters, right? But a string is already a sequence of characters. You can call reversed on it, you can loop over it, you can slice it, etc. So, this is unnecessary.

And, if you've left a as a string, the slice a[::-1] is also a string.

That means your whole program can reduce to this:

a = input('Please enter a string: ')

# read the string backwards
b = a[::-1]

# append the backward-ordered string to the original string, and print this new string
c = a + b

print(c)

Or, more simply:

a = input('Please enter a string: ')

print(a + a[::-1])

1 Comment

great answer! then please ignore my reply to your comment above. Thank you for this elegant solution! I'm sure that i have a lot to learn!
1

You can do this:

in_str = input('Please enter a string: ')
a = list(in_str)
b=a+a[::-1]
print (''.join(b))

Prints:

Please enter a string: test
testtset

And there is actually no reason to convert to a list first for this case since you can index, reverse and concatenate the string directly in Python:

>>> s='test'
>>> s+s[::-1]
'testtset'

Which shows a common idiom in Python to test if a string is a palindrome:

>>> pal='tattarrattat'
>>> pal==pal[::-1]
True

1 Comment

Why convert the string to a list? Strings are already sliceable sequences of characters, so the same thing would work without the conversion—and you wouldn't need to join at the end.
0
def make_palindrome(string):
    return string + string[::-1]

make_palindrome(input('Please enter a String: '))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.