首页 > 解决方案 > 我们如何将多个字符串连接在一起?

问题描述

我正在尝试创建一个看起来像这样的字符串。

my_string = ''.join('place_id': 'x', 'licence': 'x', 'osm_type': 'X', 'osm_id': 'X', 'boundingbox': 'X', 'lat': 'X', 'lon': 'X')

或这个:

my_string = str('place_id': 'x', 'licence': 'x', 'osm_type': 'X', 'osm_id': 'X', 'boundingbox': 'X', 'lat': 'X', 'lon': 'X')

我不断收到有关无效语法的错误。我认为:是导致问题的角色,但我不知道。有人可以指出我的错误,因为我没有看到它。

我的预期结果是一个字符串:

'place_id': 231350287, 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. osm.org/copyright', 'osm_type': 'way', 'osm_id': 701902497, 'boundingbox': ['41.4689704', '41.4693896', '-81.9377844', '-81.9371249'], 'lat': '41.46913635', 'lon': '-81.93749898507869'

标签: pythonpython-3.x

解决方案


你不能:直接放在里面join

我建议你做这样的事情:

my_string = ', '.join(['place_id'+': '+'x', 'licence'+': '+'x', 'osm_type'+': '+'X', 'osm_id'+': '+'X', 'boundingbox'+': '+'X', 'lat'+': '+'X', 'lon'+': '+'X'])

或这个 :

my_string = ', '.join(['place_id: ' + 'x', 'licence: ' + 'x', 'osm_type: ' + 'X', 'osm_id: ' + 'X', 'boundingbox: ' + 'X', 'lat: ' + 'X', 'lon: ' + 'X'])

很丑,但应该可以


推荐阅读