Operating environment ・ Windows 10 ・ Python2.7
I used to concatenate strings with +, but I heard that it would affect speed and use a lot of memory, so I thought there was a good way to do it, so I searched for it.
Test1.py
# -*- conding: utf-8 -*-
class Test():
def __init__(self):
#Create a list to store strings
self.testList = []
def main(self):
self.testList.append("abc")
self.testList.append("def")
#Concatenate elements that are not listed with a single-byte space
self.testStr = " ".join(self.testList)
print self.testStr
if __name__ == '__main__':
test = Test()
test.main()
Test1.py output
abc def
It seems that this method can be fast! In java, there is StringBuilder, but it looks similar.
I tried the method of comment from @ _ha1f!
Test2.py
# -*- conding: utf-8 -*-
if __name__ == '__main__':
print "{} {}".format("abc", "def")
print u"{} {}".format(u"Ah", u"Eoka")
Test2.py output
abc def
Ah Eoka
I also changed the variable name to something other than List or str.
Reference site HDE Lab Super Word Theory
Recommended Posts