简体   繁体   中英

Append a string to a list of unicode strings

What I've tried:

>> abcd = [u'abcd']
>> abcd_ef = abcd + 'ef'
>> abcd_ef

[u'abcd', 'e', 'f']

What I'd like:

>> abcd = [u'abcd']
>> abcd_ef = **MAGIC ???**
>> abcd_ef

[u'abcd', 'ef']

Hopefully I made that clear enough!

Make it a list:

>>> abcd = [u'abcd']
>>> abcd_ef = abcd + ['ef']
>>> abcd_ef
[u'abcd', 'ef']

otherwise the list adds each element (eg each character) of the string separately.

Alternatively, you can call .append() on abcd and modify that list in-place:

>>> abcd = [u'abcd']
>>> abcd.append('ef')
>>> abcd
[u'abcd', 'ef']

This is all standard python list manipulation and is independent of the contents; it doesn't matter if there are unicode objects or custom objects in that list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM