Reputation: 35
I have created a program which chooses random items from lists imported from text documents that are associated with the topic e.g. food in eats contains pizza,pasta and burgers. However, when it prints out the result the chosen items have parentheses, square brackets and speech marks. how would i remove them?
things_to_do=[
("eat", [(foods[randint(0,20)])]),
("do", [(sports[randint(0,60)])]),
("drink a",[(coffees[randint(0,20)])])]
print "Whilst in town you decided to " + str(things_to_do[randint(0,2)])]
Upvotes: 1
Views: 1106
Reputation: 73450
These brackets, quotes, etc. are just part of the string representation of more complex data structures such as lists
or tuples
. You need to properly prepare/format your data for nicer output:
things_to_do = [
("eat", foods[randint(0,20)]), # less complex than the singleton lists in your code
("do", sports[randint(0,60)]),
("drink a", coffees[randint(0,20)])
]
verb, obj = things_to_do[randint(0,2)]
print "Whilst in town you decided to {v} {o}".format(v=verb, o=obj)
String formatting in the docs.
Upvotes: 4
Reputation: 375
It's because you are not printing strings but tuples, and lists.
print "Hi my name is "+str(("really",["Tom"]))
leads to
Hi my name is ('really', ['Tom'])
So you want to access/manipulate your variables to print strings
Upvotes: 0
Reputation: 84
Use yourVar[start, end]
to print your string between the start and the end character, for example if my text is myVar = "[(hello)]"
myVar[3:-3]
will show hello
Upvotes: 0