在python中将两个字符串加在一起
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/21589662/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding two strings together in python
提问by Lee
I'm trying to place two strings together but when I run this code I keep getting an error. I'm sure this is something basic but I ahave been playing with this for 30 minutes and can't figure out what's wrong
我试图将两个字符串放在一起,但是当我运行此代码时,我不断收到错误消息。我确定这是一些基本的东西,但我已经玩了 30 分钟,但不知道出了什么问题
filename= 'data.txt'
1output = '1min' + filename
采纳答案by Lee
Like most languages, Python does not allow you to create a name that starts with a number.  This means that you need to rename 1outputbecause its name is illegal:
与大多数语言一样,Python 不允许您创建以数字开头的名称。这意味着您需要重命名,1output因为它的名称是非法的:
output1 = '1min' + filename
Below is a demonstration:
下面是一个演示:
>>> filename = 'data.txt'
>>> 1output = '1min' + filename
  File "<stdin>", line 1
    1output = '1min' + filename
          ^
SyntaxError: invalid syntax
>>>
>>> filename = 'data.txt'
>>> output1 = '1min' + filename
>>> output1
'1mindata.txt'
>>>
When creating names in Python, you must obey the following rules*:
在 Python 中创建名称时,必须遵守以下规则*:
The first character must be either a letter or an underscore.
The rest of the characters must be letters, underscores, and/or numbers.
The finished name cannot be the same as one of the keywords(
if,def,for, etc.).
第一个字符必须是字母或下划线。
其余字符必须是字母、下划线和/或数字。
成品名称不能是一样的一个关键字(
if,def,for等)。
*Note:In addition, you should refrain from creating a name that is the same as one of the built-in functions(str, input, list, etc.).  While doing so islegal, it is considered a bad practice by many Python coders (author included).  This is because it will overshadow the built-in and thereby make it unusable in the current scope.
*注:另外,你应该从创建一个名称相同的一个避免内置功能(str,input,list等)。虽然这样做是合法的,但许多 Python 编码人员(包括作者)认为这是一种不好的做法。这是因为它会掩盖内置函数,从而使其在当前作用域中无法使用。

