Reputation: 35
I'm creating a google searcher in python. Is there any way that I can replace a space in a list with a "+" for my url? This is my code so far:
q=input("Question=")
qlist=list(q)
#print(qlist)
Can I replace any spaces in my list with a plus, and then turn that back into a string?
Upvotes: 0
Views: 255
Reputation: 2348
Just want to add another line of thought there. Try the urllib library for parsing url strings.
Here's an example:
import urllib
## Create an empty dictionary to hold values (for questions and answers).
data = dict()
## Sample input
input = 'This is my question'
### Data key can be 'Question'
data['Question='] = input
### We'll pass that dictionary hrough the urlencode method
url_values = urllib.parse.urlencode(data)
### And print results
print(url_values)
#-------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------
#Alternatively, you can setup the dictionary a little better if you only have a couple of key-value pairs
## Input
input = 'This is my question'
# Our dictionary; We can set the input value as the value to the Question key
data = {
'Question=': input
}
print(urllib.parse.urlencode(data))
Output:
'Question%3D=This+is+my+question'
Upvotes: 1
Reputation: 308
Look at the join and split operations in python.
q = 'dog cat'
list_info = q.split()
https://docs.python.org/3/library/stdtypes.html#str.split
q = ['dog', 'cat']
s_info = ''.join(q)
https://docs.python.org/3/library/stdtypes.html#str.join
Upvotes: 0
Reputation: 10682
You can just join it together to create 1 long string.
qlist = my_string.split(" ")
result = "+".join(qlist)
print("Output string: {}".format(result))
Upvotes: 0