str = "now, I' learning python."
print(str.find("python"))
'''
17
'''
str = "--";
seq = ("a", "b", "c"); # 字符串序列
print (str.join( seq ));
str1 = "--"
str1 = ("a", "b", "c");# 字符串序列
str2 =("nia", "sdfg", "454");
print(str.join(str1))
print(str.join(str2))
'''
a--b--c
a--b--c
nia--sdfg--454
'''
连接的好像必须是名为str的字符串,名字为其他字符串会报错
str1 = "pasHJKdsfklSKkJKL";
print (str1.lower()) # 以空格为分隔符,包含 \n
print (str1.upper()) # 以空格为分隔符,分隔成两个
'''
pashjkdsfklskkjkl
PASHJKDSFKLSKKJKL
'''
用指定字符替换指定字符(也可以是字符串):
str1 = "pasHJKdsfklSKkJKL";
print (str1.replace("k", "4")) # 以空格为分隔符,包含 \n
print (str1.upper()) # 以空格为分隔符,分隔成两个
'''
pasHJKdsf4lSK4JKL
PASHJKDSFKLSKKJKL
'''
str.split(str="", num=string.count(str)).
str – 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
num – 分割次数。默认为 -1, 即分隔所有。
str1 = "Line1-abcdef \n Line2-abc \n Line4-abcd";
print (str1.split( )); # 以空格为分隔符,包含 \n
print (str1.split(' ', 1 )); # 以空格为分隔符,分隔成两个
'''
a--b--c
a--b--c
nia--sdfg--454
'''
任何进制之间都可以转换。
x = 0b1010101;
print(oct(x), bin(x), x, hex(x))
x = 0o1024;
print(oct(x), bin(x), x, hex(x))
x = 1024;
print(oct(x), bin(x), x, hex(x))
x = 0x1024;
print(oct(x), bin(x), x, hex(x))
'''
输出:
0o125 0b1010101 85 0x55
0o1024 0b1000010100 532 0x214
0o2000 0b10000000000 1024 0x400
0o10044 0b1000000100100 4132 0x1024
'''
x = 0b1010101;
print('{0:b}'.format(x), '{0:o}'.format(x), '{0:x}'.format(x))
x = 0o1024;
print('{0:b}'.format(x), '{0:o}'.format(x), '{0:x}'.format(x))
x = 1024;
print('{0:b}'.format(x), '{0:o}'.format(x), '{0:x}'.format(x))
x = 0x1024;
print('{0:b}'.format(x), '{0:o}'.format(x), '{0:x}'.format(x))
'''
输出:
1010101 125 55
1000010100 1024 214
10000000000 2000 400
1000000100100 10044 1024
'''
hex函数比格式化字符串函数format慢,不推荐使用.
x = "0b1010"
y = "0xc"
z = "0o10"
print(int(x, 2), int(y, 16), int(z, 8))
'''
输出:
10 12 8
'''
(上面一个是字符串,下面一个是数字)
x = 0b1010
y = 0xc
z = 0o10
print(’{0:d}’.format(x), ‘{0:d}’.format(y), ‘{0:d}’.format(z))
'''
输出:
10 12 8
'''
(注意这个又是字符串)
x = ‘0b1010’
y = ‘0xc’
z = ‘0o10’
print(eval(x), eval(y), eval(z))
'''
输出:
10 12 8
'''