#!/usr/bin/python
#-*- coding:utf8 -*-
#本章主要讲下字符串的操作
#首先介绍字符串的格式化
str1 = "Jack"
str2 = "WJin"
print "%s "% str1
print "%s ^_^ %s" % (str1, str2)
print "关于小数: %f--%.1f--%.2f" % (1.35, 1.35, 1.335)
#第一个精度更高
print "%(name)s: %(age)d" % {"name":"Smith", "age":23}
#字符串在屏幕中显示的位置
str3 = "Version3.0"
print str3.center(40)
print str3.center(50)
print str3.ljust(0)
print str3.rjust(20)
filename = "/home/shell/test01/show.sh\n"
print len(filename)
filename = r"/home/shell/test01/show.sh\n"
print len(filename)
#使用r的区别
str4 = "\r\nRequest error\r\n\r\n"
print str4.strip() #此时会没有空行
print str4.lstrip() #此时右边有空行
print str4.rstrip() #此时左边有空行
print str4
#重载加法运算符
str5 = "Hello "
str6 = "Jack"
str7 = str5 + str6 + " welcome"
print str7
#进行链接
test_list = ["This", "is", "a", "list","test","from", "jack"]
result = " ".join(test_list) #将所有字串链接起来 空格分开
print result
import operator
test_list2 = ["This", " is"," another"," test"]
result = reduce(operator.add, test_list2, "")
print result
#reduce的效果没有join好,不能空格分割
#现在讲讲分割和子串
str8 = "11-22-33-44-55"
print str8.split()
print str8.split("-")
print str8.split("-", 1)
print str8.split("-",2)
str9 = "Welcome to China"
print str9[1:2]
print str9[1:3]
print str9[1:2:2]
print str9[-2:-1]
#现在介绍下字符串的比较
str10 = 1
str11 = "1"
if str10 == str11:
print "YES"
else:
print "NO"
#肯定是NO 是吧
print str9.startswith("Wel")
print str9.endswith("ina", 5)
print str9.endswith("ina", 5,15)
print str9.endswith("ina", 10, len(str9))
#教你多种方法进行字符串的反转
def reverse(s):
return s[::-1] #这是最简单的了
def reverse1(s):
test_list1 = list(s)
test_list1.reverse()
s = "".join(test_list1)
return s
def reverse2(s):
ss = ""
test_list2 = list(s)
for i in range(len(test_list2), 0, -1):
ss += "".join(test_list2[i-1])
return ss
print reverse("Hello")
print
print reverse1("Hello")
print
print reverse2("Hello")
print
print
#进行字符差的查找
str13 = "Ths is a test from jack, jack ,jack"
print str13.find("a")
print str13.find("is")
#返回查找时的位置
#进行替换
print str13.replace("jack","Smith") #全局替换
print str13.replace("jack","Smith",2) #局部替换
#时间格式化
import time,datetime
print time.strftime("%Y-%m-%d-%x", time.localtime())
t = time.strptime("2014-9-17","%Y-%m-%d")
y,m,d = t[0:3]
print datetime.datetime(y,m,d)
#-*- coding:utf8 -*-
#本章主要讲下字符串的操作
#首先介绍字符串的格式化
str1 = "Jack"
str2 = "WJin"
print "%s "% str1
print "%s ^_^ %s" % (str1, str2)
print "关于小数: %f--%.1f--%.2f" % (1.35, 1.35, 1.335)
#第一个精度更高
print "%(name)s: %(age)d" % {"name":"Smith", "age":23}
#字符串在屏幕中显示的位置
str3 = "Version3.0"
print str3.center(40)
print str3.center(50)
print str3.ljust(0)
print str3.rjust(20)
filename = "/home/shell/test01/show.sh\n"
print len(filename)
filename = r"/home/shell/test01/show.sh\n"
print len(filename)
#使用r的区别
str4 = "\r\nRequest error\r\n\r\n"
print str4.strip() #此时会没有空行
print str4.lstrip() #此时右边有空行
print str4.rstrip() #此时左边有空行
print str4
#重载加法运算符
str5 = "Hello "
str6 = "Jack"
str7 = str5 + str6 + " welcome"
print str7
#进行链接
test_list = ["This", "is", "a", "list","test","from", "jack"]
result = " ".join(test_list) #将所有字串链接起来 空格分开
print result
import operator
test_list2 = ["This", " is"," another"," test"]
result = reduce(operator.add, test_list2, "")
print result
#reduce的效果没有join好,不能空格分割
#现在讲讲分割和子串
str8 = "11-22-33-44-55"
print str8.split()
print str8.split("-")
print str8.split("-", 1)
print str8.split("-",2)
str9 = "Welcome to China"
print str9[1:2]
print str9[1:3]
print str9[1:2:2]
print str9[-2:-1]
#现在介绍下字符串的比较
str10 = 1
str11 = "1"
if str10 == str11:
print "YES"
else:
print "NO"
#肯定是NO 是吧
print str9.startswith("Wel")
print str9.endswith("ina", 5)
print str9.endswith("ina", 5,15)
print str9.endswith("ina", 10, len(str9))
#教你多种方法进行字符串的反转
def reverse(s):
return s[::-1] #这是最简单的了
def reverse1(s):
test_list1 = list(s)
test_list1.reverse()
s = "".join(test_list1)
return s
def reverse2(s):
ss = ""
test_list2 = list(s)
for i in range(len(test_list2), 0, -1):
ss += "".join(test_list2[i-1])
return ss
print reverse("Hello")
print reverse1("Hello")
print reverse2("Hello")
#进行字符差的查找
str13 = "Ths is a test from jack, jack ,jack"
print str13.find("a")
print str13.find("is")
#返回查找时的位置
#进行替换
print str13.replace("jack","Smith") #全局替换
print str13.replace("jack","Smith",2) #局部替换
#时间格式化
import time,datetime
print time.strftime("%Y-%m-%d-%x", time.localtime())
t = time.strptime("2014-9-17","%Y-%m-%d")
y,m,d = t[0:3]
print datetime.datetime(y,m,d)