吴裕雄--天生自然 PYTHON3开发学习:字符串

var1 = 'Hello World!'
var2 = "Runoob"
#!/usr/bin/python3
 
var1 = 'Hello World!'
var2 = "Runoob"
 
print ("var1[0]: ", var1[0])
print ("var2[1:5]: ", var2[1:5])
#!/usr/bin/python3
 
var1 = 'Hello World!'
 
print ("已更新字符串 : ", var1[:6] + 'Runoob!')
#!/usr/bin/python3
 
a = "Hello"
b = "Python"
 
print("a + b 输出结果:", a + b)
print("a * 2 输出结果:", a * 2)
print("a[1] 输出结果:", a[1])
print("a[1:4] 输出结果:", a[1:4])
 
if( "H" in a) :
    print("H 在变量 a 中")
else :
    print("H 不在变量 a 中")
 
if( "M" not in a) :
    print("M 不在变量 a 中")
else :
    print("M 在变量 a 中")
 
print (r'\n')
print (R'\n')
#!/usr/bin/python3
 
print ("我叫 %s 今年 %d 岁!" % ('小明', 10))
#!/usr/bin/python3
 
para_str = """这是一个多行字符串的实例
多行字符串可以使用制表符
TAB ( \t )。
也可以使用换行符 [ \n ]。
"""
print (para_str)
errHTML = '''
<HTML><HEAD><TITLE>
Friends CGI Demo</TITLE></HEAD>
<BODY><H3>ERROR</H3>
<B>%s</B><P>
<FORM><INPUT TYPE=button VALUE=Back
ONCLICK="window.history.back()"></FORM>
</BODY></HTML>
'''
cursor.execute('''
CREATE TABLE users (  
login VARCHAR(8), 
uid INTEGER,
prid INTEGER)
''')
#!/usr/bin/python3

str = "this is string example from runoob....wow!!!"

print ("str.capitalize() : ", str.capitalize())
#!/usr/bin/python3

str = "[www.runoob.com]"

print ("str.center(40, '*') : ", str.center(40, '*'))
str="www.runoob.com"
sub='o'
print ("str.count('o') : ", str.count(sub))

sub='run'
print ("str.count('run', 0, 10) : ", str.count(sub,0,10))
str = "菜鸟教程";
str_utf8 = str.encode("UTF-8")
str_gbk = str.encode("GBK")
 
print(str)
 
print("UTF-8 编码:", str_utf8)
print("GBK 编码:", str_gbk)
 
print("UTF-8 解码:", str_utf8.decode('UTF-8','strict'))
print("GBK 解码:", str_gbk.decode('GBK','strict'))
#!/usr/bin/python3
 
str1 = "Runoob example....wow!!!"
str2 = "exam";
 
print (str1.find(str2))
print (str1.find(str2, 5))
print (str1.find(str2, 10))
str1 = "Runoob example....wow!!!"
str2 = "exam";

print (str1.index(str2))
print (str1.index(str2, 5))
print (str1.index(str2, 10))
str = "runoob2016"  # 字符串没有空格
print (str.isalnum())
 
str = "www.runoob.com"
print (str.isalnum())
str = "runoob"
print (str.isalpha())

str = "Runoob example....wow!!!"
print (str.isalpha())
str = "123456"; 
print (str.isdigit())

str = "Runoob example....wow!!!"
print (str.isdigit())
str = "RUNOOB example....wow!!!"
print (str.islower())

str = "runoob example....wow!!!"
print (str.islower())
str = "runoob2016"  
print (str.isnumeric())

str = "23443434"
print (str.isnumeric())
str = "       " 
print (str.isspace())
 
str = "Runoob example....wow!!!"
print (str.isspace())
str = "THIS IS STRING EXAMPLE....WOW!!!"
print (str.isupper())

str = "THIS is string example....wow!!!"
print (str.isupper())
s1 = "-"
s2 = ""
seq = ("r", "u", "n", "o", "o", "b") # 字符串序列
print (s1.join( seq ))
print (s2.join( seq ))
str = "Runoob example....wow!!!"

print (str.ljust(50, '*'))
str = "Runoob EXAMPLE....WOW!!!"

print( str.lower() )
str = "Runoob example....wow!!!"

print (str.ljust(50, '*'))
str = "runoob"
print ("最大字符: " + max(str))
str = "this is string example....wow!!!"
print (str.split( ))       # 以空格为分隔符
print (str.split('i',1))   # 以 i 为分隔符
print (str.split('w'))     # 以 w 为分隔符
str = "this is string example....wow!!!"
print (str.startswith( 'this' ))   # 字符串是否以 this 开头
print (str.startswith( 'string', 8 ))  # 从第八个字符开始的字符串是否以 string 开头
print (str.startswith( 'this', 2, 4 )) # 从第2个字符开始到第四个字符结束的字符串是否以 this 开头
str = "*****this is **string** example....wow!!!*****"
print (str.strip( '*' ))  # 指定字符串 *
str = "123abcrunoob321"
print (str.strip( '12' ))  # 字符序列为 12
str = "this is string example from runoob....wow!!!";

print ("str.upper() : ", str.upper())

猜你喜欢

转载自www.cnblogs.com/tszr/p/10963146.html
今日推荐