Reputation: 243
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!
Upvotes: 1
Views: 4228
Reputation: 117
def make_palindrome(string):
return string + string[::-1]
make_palindrome(input('Please enter a String: '))
Upvotes: 0
Reputation: 103754
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
Upvotes: 1
Reputation: 365657
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])
Upvotes: 1
Reputation: 771
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
Upvotes: 5