python中如何将list转换成字符串
看你想转成什么样的字符串了。list1=["a","b"]str(list1) : 这种的只会把列表转成字符串,样子还是["a","b"]''.join(list1) :这种的会转成字符串 'ab'
python如何把输进去的字符倒过来
其中的一种实现方法,使用 str 的 join() 方法就可以实现。大概的步骤是:
1.把数字转为字符串, 如果是使用 input() 函数接受输入,本身就是一个字符串,否则可以使用 str() 转;
2.使用逗号(,)连接符连接字符串;
3.使用切片倒叙输出。思路大概是这样的,请上机调试。
python中特殊字符的替换
1、用字符串本身的replace方法
复制代码代码如下:
a.replace('word','python')
输出的结果是hello python
2、用正则表达式来完成替换:
复制代码代码如下:
import re
strinfo = re.compile('word')
b = strinfo.sub('python',a)
print b
输出的结果也是hello python
至于用哪个方法的话,看你自己的选择了。
Python如何将Unicode中文字符串转换成string字符串
普通字符串可以用多种方式编码成Unicode字符串,具体要看你究竟选择了哪种编码:unicodestring=u"Helloworld"#将Unicode转化为普通Python字符串:"encode"utf8string=unicodestring.encode("utf-8"
)asciistring=unicodestring.encode("ascii"
)isostring=unicodestring.encode("ISO-8859-1"
)utf16string=unicodestring.encode("utf-16")#将普通Python字符串转化为Unicode:"decode"plainstring1=unicode(utf8string,"utf-8"
)plainstring2=unicode(asciistring,"ascii"
)plainstring3=unicode(isostring,"ISO-8859-1"
)plainstring4=unicode(utf16string,"utf-16"
)assertplainstring1==plainstring2==plainstring3==plainstring4
(python)如何将字符串转换成list
>>>str='2257,4018,1096'>>>target_list=[int(x)forxinstr.split(',')]>>>printtarget_list[2257,4018,1096]>>>或者>>>str='2257,4018,1096'>>>exec("target_list=[%s]"%str)>>>printtarget_list[2257,4018,1096]>>>
还没有评论,来说两句吧...