【脚本语言系列】关于Python基础知识处理字符串,你需要知道的事
2017-06-08 15:31
1131 查看
如何进行字符串处理
字符串的格式化
# -*- coding: utf-8 -*- # format the string str1 = "version" num = 1.0 format = "% s" % str1 print format format = "% s % d" % (str1, num) print format
version version 1
# -*- coding: utf-8 -*- # align the string word = "version3.0" print word.center(20) print word.center(20, "*") print word.ljust(0) print word.rjust(20) print "% 30s" %word
version3.0 *****version3.0***** version3.0 version3.0 version3.0
字符串的转义符
# -*- coding: utf-8 -*- # print the escape character path = "hello\tworld\n" print path print len(path) path = r"hello\tworld\n" print path print len(path)
hello world 12 hello\tworld\n 14
# -*- coding: utf-8 -*- # remove the escape character word = "\thello world\n" print "direct output:", word print "after strip(): ", word.strip() print "after lstrip(): ", word.lstrip() print "after rstrip(): ", word.rstrip()
direct output: hello world after strip(): hello world after lstrip(): hello world after rstrip(): hello world
字符串的合并
# -*- coding: utf-8 -*- # connect the string str1 = "hello " str2 = "world " str3 = "hello " str4 = "China " result = str1 + str2 + str3 result += str4 print result
hello world hello China
# -*- coding: utf-8 -*- # connect the string strs = ["hello ", "world ", "hello ", "China "] result = "".join(strs) print result
hello world hello China
# -*- coding: utf-8 -*- # connect the string import operator strs = ["hello ", "world ", "hello ", "China "] result = reduce(operator.add, strs, "") print result
hello world hello China
字符串的截取
# -*- coding: utf-8 -*- # split the string word = "world" print word[4]
d
# -*- coding: utf-8 -*- # split the string word = "world" str1 = "hello world" print word[0:3] print str1[::2] print str1[1::2]
wor
hlowrdel ol
# -*- coding: utf-8 -*- # split the string sentence = "Bob said: 1,2,3,4" print "blankspace split:", sentence.split() print "comma split:", sentence.split(",") print "two comma split:", sentence.split(",",2)
blankspace split: ['Bob', 'said:', '1,2,3,4'] comma split: ['Bob said: 1', '2', '3', '4'] two comma split: ['Bob said: 1', '2', '3,4']
# -*- coding: utf-8 -*- # create new space str1 = "a" print id(str1) print id(str1+"b")
55656496 86005960
字符串的比较
# -*- coding: utf-8 -*- # compare the string str1 = 1 str2 = "1" if str1 == str2: print "same" else: print "diff" if str(str1) == str2: print "same" else: print "diff"
diff same
# -*- coding: utf-8 -*- # compare the string word = "hello world" print "hello" == word[0:5] print word.startswith("hello") print word.endswith("ld",6) print word.endswith("ld",6, 10) print word.endswith("ld",6, len(word))
True True True False True
字符串的反转
# -*- coding: utf-8 -*- # reverse the string def reverse(s): out = "" li = list(s) for i in range(len(li),0,-1): out += "".join(li[i-1]) return out print reverse("hello")
olleh
# -*- coding: utf-8 -*- # reverse the string def reverse(s): out = "" li = list(s) li.reverse() s = "".join(li) return s print reverse("hello")
olleh
# -*- coding: utf-8 -*- # reverse the string def reverse(s): return s[::-1] print reverse("hello")
olleh
字符串的查找和替换
# -*- coding: utf-8 -*- # find the string sentence = "This is a apple." print sentence.find("a") sentence = "This is a apple." print sentence.rfind("a")
8 10
# -*- coding: utf-8 -*- # replace the string sentence = "hello world, hello China" print sentence.replace("hello","hi") print sentence.replace("hello","hi",1) print sentence.replace("abc", "hi")
hi world, hi China hi world, hello China hello world, hello China
字符串与日期的转换
# -*- coding: utf-8 -*- # convert between string and time import time, datetime # time to string print time.strftime("%Y-%m-%d %X", time.localtime()) # string to time t = time.strptime("2008-08-08", "%Y-%m-%d") y,m,d=t[0:3] print datetime.datetime(y,m,d)
2017-06-08 15:26:35 2008-08-08 00:00:00
相关文章推荐
- 【脚本语言系列】关于Python基础知识异常处理,你需要知道的事
- 【脚本语言系列】关于Python基础知识多返回值,你需要知道的事
- 【脚本语言系列】关于Python基础知识函数缓存,你需要知道的事
- 【脚本语言系列】关于Python基础知识调试,你需要知道的事
- 【脚本语言系列】关于Python基础知识魔法变量,你需要知道的事
- 【脚本语言系列】关于Python基础知识Lambda表达式,你需要知道的事
- 【脚本语言系列】关于Python基础知识枚举,你需要知道的事
- 【脚本语言系列】关于Python基础知识对象自省,你需要知道的事
- 【脚本语言系列】关于Python基础知识推导式,你需要知道的事
- 【脚本语言系列】关于Python基础知识__slots__,你需要知道的事
- 【脚本语言系列】关于Python基础知识文件操作,你需要知道的事
- 【脚本语言系列】关于Python基础知识装饰器,你需要知道的事
- 【脚本语言系列】关于Python基础知识容器,你需要知道的事
- 【脚本语言系列】关于Python基础知识兼容Python 2.x+Python 3.x,你需要知道的事
- 【脚本语言系列】关于Python基础知识面向对象编程,你需要知道的事
- 【脚本语言系列】关于Python基础知识设计模式,你需要知道的事
- 【脚本语言系列】关于Python基础知识协程,你需要知道的事
- 【脚本语言系列】关于Python基础知识映射器和过滤器,你需要知道的事
- 【脚本语言系列】关于Python基础知识迭代器,你需要知道的事
- 【脚本语言系列】关于 Python 错误处理,你需要知道的事