I'm pretty new to Python and am completely confused by .join()
which I have read is the preferred method for concatenating strings.
我对Python非常陌生,并且完全被.join()所迷惑,我已经读过它是连接字符串的首选方法。
I tried:
我试着:
strid = repr(595)
print array.array('c', random.sample(string.ascii_letters, 20 - len(strid)))
.tostring().join(strid)
and got something like:
和有类似:
5wlfgALGbXOahekxSs9wlfgALGbXOahekxSs5
Why does it work like this? Shouldn't the 595
just be automatically appended?
为什么它是这样工作的?595不是应该自动添加吗?
6 个解决方案
#1
256
Look carefully at your output:
仔细看看你的输出:
5wlfgALGbXOahekxSs9wlfgALGbXOahekxSs5
^ ^ ^
I've highlighted the "5", "9", "5" of your original string. The Python join()
method is a string method, and takes a list of things to join with the string. A simpler example might help explain:
我已经突出显示了原始字符串的“5”、“9”和“5”。Python join()方法是一个字符串方法,并获取与字符串连接的对象列表。一个简单的例子可能有助于解释:
>>> ",".join(["a", "b", "c"])
'a,b,c'
The "," is inserted between each element of the given list. In your case, your "list" is the string representation "595", which is treated as the list ["5", "9", "5"].
在给定列表的每个元素之间插入“,”。在您的例子中,“list”是字符串表示“595”,它被视为列表[“5”、“9”、“5”]。
It appears that you're looking for +
instead:
看起来你在寻找+代替:
print array.array('c', random.sample(string.ascii_letters, 20 - len(strid)))
.tostring() + strid
#2
78
join
takes an iterable thing as an argument. Usually it's a list. The problem in your case is that a string is itself iterable, giving out each character in turn. Your code breaks down to this:
join使用可迭代的东西作为参数。通常是一个列表。在您的例子中,问题是字符串本身是可迭代的,依次给出每个字符。你的代码是这样的:
"wlfgALGbXOahekxSs".join("595")
which acts the same as this:
其作用与此相同:
"wlfgALGbXOahekxSs".join(["5", "9", "5"])
and so produces your string:
这样就产生了你的弦
"5wlfgALGbXOahekxSs9wlfgALGbXOahekxSs5"
Strings as iterables is one of the most confusing beginning issues with Python.
作为可迭代的字符串是Python最令人困惑的开始问题之一。
#3
45
To append a string, just concatenate it with the +
sign.
要附加一个字符串,只需将它与+号连接起来。
E.g.
如。
>>> a = "Hello, "
>>> b = "world"
>>> str = a + b
>>> print str
Hello, world
join
connects strings together with a separator. The separator is what you place right before the join
. E.g.
连接用分隔符连接字符串。分隔符是您在联接之前放置的。如。
>>> "-".join([a,b])
'Hello, -world'
Join takes a list of strings as a parameter.
联接以字符串列表作为参数。
#4
7
join() is for concatenating all list elements. For concatenating just two strings "+" would make more sense:
join()用于连接所有列表元素。连接两个字符串“+”会更有意义:
strid = repr(595)
print array.array('c', random.sample(string.ascii_letters, 20 - len(strid)))
.tostring() + strid
#5
4
To expand a bit more on what others are saying, if you wanted to use join to simply concatenate your two strings, you would do this:
为了进一步扩展其他人的观点,如果您想使用join来简单地连接两个字符串,您可以这样做:
strid = repr(595)
print ''.join([array.array('c', random.sample(string.ascii_letters, 20 - len(strid)))
.tostring(), strid])
#6
0
On providing this as input ,
作为输入,
li = ['server=mpilgrim', 'uid=sa', 'database=master', 'pwd=secret']
s = ";".join(li)
print(s)
Python returns this as output :
Python返回如下输出:
'server=mpilgrim;uid=sa;database=master;pwd=secret'
#1
256
Look carefully at your output:
仔细看看你的输出:
5wlfgALGbXOahekxSs9wlfgALGbXOahekxSs5
^ ^ ^
I've highlighted the "5", "9", "5" of your original string. The Python join()
method is a string method, and takes a list of things to join with the string. A simpler example might help explain:
我已经突出显示了原始字符串的“5”、“9”和“5”。Python join()方法是一个字符串方法,并获取与字符串连接的对象列表。一个简单的例子可能有助于解释:
>>> ",".join(["a", "b", "c"])
'a,b,c'
The "," is inserted between each element of the given list. In your case, your "list" is the string representation "595", which is treated as the list ["5", "9", "5"].
在给定列表的每个元素之间插入“,”。在您的例子中,“list”是字符串表示“595”,它被视为列表[“5”、“9”、“5”]。
It appears that you're looking for +
instead:
看起来你在寻找+代替:
print array.array('c', random.sample(string.ascii_letters, 20 - len(strid)))
.tostring() + strid
#2
78
join
takes an iterable thing as an argument. Usually it's a list. The problem in your case is that a string is itself iterable, giving out each character in turn. Your code breaks down to this:
join使用可迭代的东西作为参数。通常是一个列表。在您的例子中,问题是字符串本身是可迭代的,依次给出每个字符。你的代码是这样的:
"wlfgALGbXOahekxSs".join("595")
which acts the same as this:
其作用与此相同:
"wlfgALGbXOahekxSs".join(["5", "9", "5"])
and so produces your string:
这样就产生了你的弦
"5wlfgALGbXOahekxSs9wlfgALGbXOahekxSs5"
Strings as iterables is one of the most confusing beginning issues with Python.
作为可迭代的字符串是Python最令人困惑的开始问题之一。
#3
45
To append a string, just concatenate it with the +
sign.
要附加一个字符串,只需将它与+号连接起来。
E.g.
如。
>>> a = "Hello, "
>>> b = "world"
>>> str = a + b
>>> print str
Hello, world
join
connects strings together with a separator. The separator is what you place right before the join
. E.g.
连接用分隔符连接字符串。分隔符是您在联接之前放置的。如。
>>> "-".join([a,b])
'Hello, -world'
Join takes a list of strings as a parameter.
联接以字符串列表作为参数。
#4
7
join() is for concatenating all list elements. For concatenating just two strings "+" would make more sense:
join()用于连接所有列表元素。连接两个字符串“+”会更有意义:
strid = repr(595)
print array.array('c', random.sample(string.ascii_letters, 20 - len(strid)))
.tostring() + strid
#5
4
To expand a bit more on what others are saying, if you wanted to use join to simply concatenate your two strings, you would do this:
为了进一步扩展其他人的观点,如果您想使用join来简单地连接两个字符串,您可以这样做:
strid = repr(595)
print ''.join([array.array('c', random.sample(string.ascii_letters, 20 - len(strid)))
.tostring(), strid])
#6
0
On providing this as input ,
作为输入,
li = ['server=mpilgrim', 'uid=sa', 'database=master', 'pwd=secret']
s = ";".join(li)
print(s)
Python returns this as output :
Python返回如下输出:
'server=mpilgrim;uid=sa;database=master;pwd=secret'