python笔记二

1.调用系统系统命令

import os

"""
判断系统类型
"""
# print(os.name)
# import os
#
# cmd = 'ifconfig'
# retval = os.system(cmd)
# print(retval)

"""
也是去执行一个命令,不过相比system(cmd),os.popen(command [, mode='r' [, bufsize]]),参数更多,而且是开启一个管道去执行。

    cmd - 执行的命令
    mode - 模式权限可以是'r'(默认)或'w'
    buffering - 0意味着无缓冲;1意味着行缓冲;其它正值表示使用参数大小的缓冲(大概值,以字节为单位)。负的bufsize意味着使用系统的默认值,一般来说,对于tty设备,它是行缓冲;对于其它文件,它是全缓冲。如果没有改参数,使用系统的默认值。(不明白????)


"""
# cmd = 'mkdir nwdir'
# a = os.popen(cmd)
# print(a)
# print(type(a))
# print(os.system('pwd'))

"""
打印指定目录的文件,返回一个列表
"""
# a = os.listdir('/')
# print(a)

"""
路径
"""
# a = os.getcwd()
# print(a)
# b = os.system('pwd')
# print(b)

"""
改变路径
"""
# print(os.getcwd())
#
# os.chdir('/tmp/')
#
# print(os.getcwd())

2.格式化输出

\t      //制表符
\n    //换行
\    //转义符号

print('hello\n world!')

print('hello\t world!\t\n' 'hell\t word')

print('hello world!\\n hello')
print('hello world\n hello')


end=''连接下一个print不换行输出

print('hello',end='+')
print('world')

a="""
    1.XXXX
    2.XXXX
    3.XXXX
    4.XXXX
"""
print(a)

3.字符串

特性

s = 'hello'
# 索引:0,1,2,3,4 (索引值是从0开始的)
print(s[0])
print(s[4])    #第五个
print(s[-1])  # 拿出字符串的最后一个字符

# 切片
print(s[0:3])  # 切片的规则 s[start:end:step] 从start开始到end-1结束,步长为step
print(s[0:4:2])
print(s[:])  # 显示所有字符
print(s[:3])  # 显示前3个字符
print(s[::-1])  # 字符串倒序
print(s[1:])  # 除了第一个字符之外,其他全部显示

# 重复
print(s * 10)

# 连接
print('hello ' + 'python')

# 成员操作符
print('he' in s)
print('aa' in s)
print('he' not in s)

 

 

 

 


str.istitle()    //判断是否为标题
str.istitle()    //修改为标题格式
str.isupper()    //判断是否为大写
str.islower()    //小写判断

strisalnum()   //判断数字和字符串
str.isdigit()        //判断数字
str.isalpha()        //判断字母

str.endswith('结尾')    //判断结尾
str.startswith('开头') //判断开头
In [22]: 'Hello'.strip('H')    //输出除了H的字符
Out[22]: 'ello'
.lstrip('H')

str.find('hello')        //查找hello子串的最小索引
str.rfind('hello')        //查找hello子串的最大索引
str.replace('hello','westos')    //替换子串

str.center(30)        //字符串对齐,其余为空格
str.center(30,'*')        //其余为*
str.ljust(30,'*')        //左对齐
str.rjust(30,'*')        //右对齐
str.ljust(30)
str.count('A')        //统计次数
len('hello')        //统计长度

str.split('.')        //字符串分离,以.分离
str.join()          //字符串连接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值