python----库函数使用、字符串

1库函数使用

库函数使用导包import
随机数random

random.randint(a,b)a到b随机整数
random.unifirm(a,b)a到b随机小数
random.randrange(a,b,c)a到b之间,间隔c的整数,相当于c的倍数

import random
a=random.randint(0,5)
print(a)
b=random.uniform(0,5)
print(b)
c=random.randrange(0,100,7)
print(c)

结果:运行第1次:4 、3.98695976224 、14
运行第2次:5、2.25040371393、63、
......

2字符串

字符串输入

raw_input()读取控制台输入

字符串输出:

%d—显示整数
%2f—显示2位小数

字符串下标:
str="hello world"
str[0] -----'h'
str[1] -----'e'
str[0:3]----'hel'
str[2:3]-----'l'
str[2:12]---'llo world'
str=[2:]---'llo world'
str=[-3:]---'rld'
str=[2:-1]--'llo worl'
字符串常见简单操作:

——-代码块为在eclipse中python代码:

1)find 返回指定字符串在原字符串中的开始位置的索引值,没有结果返回-1
str="welcome to the happy family"
a=str.find("to")
print(a)
运行结果:8

Ps:find(“xx”,a,b)在a,b范围寻找xx,找到返回开始位置的索引值,没有结果返回-1

2)index 返回指定字符串在原字符串中的开始位置的索引值,与find区别,没找到,报错
3)count 返回指定字符串在原字符串中出现的次数
str="hello world hello China"
b=str.count("hello")
print(b)
运行结果:2
4)len 字符串长度
str="hello world hello China"
c=len(str)
print(c)
运行结果:23
5)replace 把原字符串中指定一部分替换成另一部分,如果count指定,则替换不超过count次

str.replace(str1,str2)把str1替换为str2

str="hello world"
d=str.replace("world", "China")
print(d)
运行结果:hello China
6)split 切割
str="a b c d e f g"
e=str.split(" ")--引号中有一空格
print(e)
运行结果:['a', 'b', 'c', 'd', 'e', 'f', 'g']
将str中字母之间的空格去掉了
另:str="a b c d e f g"
e=str.split(" ",2)----数字表示切几次
print(e)
运行结果:['a', 'b', 'c d e f g']
7)capitalize 把字符串的第一个字母大写
str="hello world"
f=str.capitalize()
print(f)
运行结果:Hello world
8)startswith 检查字符串是否以xx开头,是返回true,否则返回false
str="welcome to the happy family"
g=str.startswith("welcome")
print(g)
运行结果:True
9)endswith 检查字符串是否以xx结尾,是返回true,否则返回false
str="welcome to the happy family"
h=str.endswith("welcome")
print(h)
运行结果:False
10)lower 把字符串中所有大写换成小写
str="DdfgdfDDdHdsgfdsRgsgGAghfsdfhSE"
i=str.lower()
print(i)
运行结果:ddfgdfdddhdsgfdsrgsggaghfsdfhse
11)upper 把字符串中所有小写换成大写
str="DdfgdfDDdHdsgfdsRgsgGAghfsdfhSE"
i=str.upper()
print(i)
运行结果:DDFGDFDDDHDSGFDSRGSGGAGHFSDFHSE
12)ljust 返回一个原字符串左对齐,并用空格补充至长度width的新字符串
str.ljust(width)
13)rjust 返回一个原字符串右对齐,并用空格补充至长度width的新字符串
str.rjust(width)
14)center 返回一个原字符串居中,并用空格补充至长度width的新字符串
str.center(width)
str="helloworld"
j=str.ljust(20)
print(j)
str="helloworld"
k=str.rjust(20)
print(k)
str="helloworld"
l=str.center(20)
print(l)
运行结果:helloworld          
              helloworld
           helloworld 
15)lstrip 删除左边的空格
str.lstrip()
16)rstrip 删除右边(末尾)的空格
str.rstrip()
str="      helloworld"
j=str.lstrip()
print(j)
str="helloworld      "
k=str.rstrip()
print(k)
运行结果:helloworld
         helloworld
17)rfind 类似于find,不过是从右边开始找
18)rindex 类似于index,不过是从右边开始找
19)partition 分割
str.partition(str1)------以str1将str分成三部分,str1前,本身,后
str="hello world and hello andChina"
m=str.partition("and")
print(m)
运行结果:('hello world ', 'and', ' hello andChina')
20)rpartition 类似于partition,不过是从右边开始
str="hello world and hello andChina "
n=str.rpartition("and")
print(n)
运行结果:('hello world and hello ', 'and', 'China ')
21)splitlines 按照行分隔,返回一个包含各行作为元素的列表
str.splitlines()
str="hello \n world "
o=str.splitlines()
print(o)
运行结果:['hello ', ' world ']
22)isdigit 字符串只包含数字,返回true,否则返回false
str.isdigit()
23)isalpha 字符串只包含字母,返回true,否则返回false
str.isalpha()
24)isalnum 字符串只包含数字或字母,返回true,否则返回false
str.isalnum()
25)isspace 字符串只包含空格,返回true,否则返回false
str.isspace()
26)islower 字符串只包含小写字母,返回true,否则返回false
str.islower()
27)isupper 字符串只包含大写字母,返回true,否则返回false
str.isupper()
28)join 在str中每个字符后插入str1,构造一个新的字符串
str.join(str1)

注意:写语句时是,需要插入的在前,原字符串在后,如下示例,在str1中插入str,正确为str.join(str1),如果写成str1.join(str)会报错

str1=["hello","world","and","hello","China"]
str=" "----空格
p=str1.join(str)
print(p)
运行结果:报错:  p=str1.join(str)
AttributeError: 'list' object has no attribute 'join'
-----------------------------------------------------------
str1=["hello","world","and","hello","China"]
str=" "
q=str.join(str1)
print(q)
运行结果:hello world and hello China
----------------
str1="hello"
str=" ==="
q=str.join(str1)
print(q)
运行结果:h ===e ===l ===l ===o
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值