Search code examples
pythongeolocationgeocoder

How to remove a certain amount of characters from a string


In python, I am making a virtual assistant. I am working on a way to get the current location of your device and display the weather in that location. Getting the location works fine. I store that data in a variable and print it, but the module I am using displays a bunch of extra information that I don't want. How can I trim off the extra characters so that it's just the city? The output is

Python: The current weather for <[OK] Ipinfo - Geocode [mycity, mystate, US]> is .

And my code is

# GeoCoder
import geocoder
g = geocoder.ip('me')
print(g)

# import modules necessary to request data from weather api
import requests
import json

print("""
      Welcome to Python Assistant.
      Check the included documentation for a list of requests.
      You can start by typing in a request.
      """)
while True:
      text = input("$PyASSIST > ") 
      # HELP
      if(text.upper() == "HELP"): 
            print("Python: Check the documentation for help.")
      # HELLO
      elif(text.upper() == "HELLO") or (text.upper() == "HI"): 
            print("Python: Hi! How are you today?")
      elif(text.upper() == "TEST") or (text.upper() == "TEST2"): 
            print("Python: The current weather for " + str(g) + " is " + ".")
      else:
            print("Python: I don't know how to respond to that.")

EDIT: Almost accidentally doxxed myself, that would have been bad


Solution

  • could try this?

    myString = 'Python: The current weather for <[OK] Ipinfo - Geocode [mycity, mystate, US]> is .'
    
    myString = myString.split('[')[2].split(',')[0]
    

    Output:

    mycity