如何从字符串中删除一定数量的字符

How to remove a certain amount of characters from a string

在python,我正在制作一个虚拟助手。我正在研究一种方法来获取您设备的当前位置并显示该位置的天气。获取位置工作正常。我将该数据存储在一个变量中并打印出来,但是我正在使用的模块显示了一堆我不想要的额外信息。我怎样才能 trim 去掉多余的字符,只显示城市? 输出是

Python: The current weather for <[OK] Ipinfo - Geocode [mycity, mystate, US]> 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.")

编辑:差点不小心打扰了自己,那会很糟糕

可以试试这个吗?

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

myString = myString.split('[')[2].split(',')[0]

输出:

mycity