[Tutor] Removing characters in a string using format()
Dave Angel
d at davea.name
Thu Jul 21 20:12:04 CEST 2011
On 07/21/2011 01:53 PM, Ryan Porter wrote:
> Hi there,
>
> In one part of a program I'm writing, I want a list to be printed to
> the string. Here's my code:
>
> # Begin snippet
> listString = input('Please enter a single item: >').strip();
>
> / print();
> itemList.append(listString);
> /
>
> /...
> /
>
> /print('And here it is in alphabetical order:', itemList)
> # End Snippet
> /
>
> However, when I print the list, I get something like this: ['Python',
> 'best', 'ever', 'is', 'language', 'programming', 'the'] with brackets.
> Is there a way to use format() to remove the brackets before the list
> is printed?
>
> Thanks for the help!
> //
itemlist isn't a string, it's presumably a list. If you pass a list to
string, it'll turn it into a string, using approximately the following
rules:
Put brackets on the end, and between them call repr() on each item of
the list, separating the items with commas.
If all you like the rest of it, but don't want the brackets, try (untested)
print('And here it is in alphabetical order:',
str(itemList).strip('[]'))
This explicitly converts the list to a string, then strips both ends of
the specified characters.
DaveA
More information about the Tutor
mailing list