day08-字符串操作
name = 'hello,world,WORLD! 123,你好'
#capitalize()
#首字母大写,其他全部变成小写,没有iscapitalize()方法
print(name.capitalize())
#Hello,world,world! 123
#title(),istitle()
#把每个单词的首字母都变成大写
print(name.title())
#Hello,World,World! 123,你好
#判断是否是首字母大写
print(name.istitle())
#False
#upper(),isupper()
#全部变成大写
print(name.upper())
#HELLO,WORLD,WORLD! 123
#判断是否全是大写
print(name.isupper())
#False
#casefold()
#全部变成小写,没有iscasefold()方法
print(name.casefold())
#hello,world,world! 123
#lower(),islower()
#全部变成小写
print(name.lower())
#hello,world,world! 123
#判断是否全是小写
print(name.islower())
#False
#casefold()和lower()的区别,lower()只对 ASCII 也就是 'A-Z'有效,但是其它一些语言里面存在小写的情况就没办法了。
#文档里面举得例子是德语中'ß'的小写是'ss'(这个我也不懂):
#s = 'ß'
#s.lower() # 'ß'
#s.casefold() # 'ss'
#总结来说,汉语 & 英语环境下面,继续用 lower()没问题;要处理其它语言且存在大小写情况的时候再用casefold()。
#center()
#总共需要占40个字符串位置,name在居中,如果不够就用-补充,如果字符串够了就不改变
print(name.center(40,'-'))
#-------hello,world,WORLD! 123,你好--------
#ljust(),rjust()
#总共需要占40个字符串位置,name居左,其余用-补齐
print(name.ljust(40,'-'))
#hello,world,WORLD! 123,你好---------------
#总共需要占40个字符串位置,name居右,其余用-补齐
print(name.rjust(40,'-'))
#---------------hello,world,WORLD! 123,你好
#count()
#统计o的个数
print(name.count('o'))
#2
#统计下标为0到5中的o的个数
print(name.count('o',0,5))
#1
#index(),find()
print(name.index('o'))
print(name.find('o'))
#2个方法都是从左开始查o在name中的位置,如果有则返回下标,如果没有则find返回-1,index会报错
#4
#从右开始查找o在name中的位置,如果有则返回下标,如果没有则find返回-1,index会报错
print(name.rfind('o'))
print(name.rindex('o'))
#startswith()
#判断是否以某个字符或字符串开关
print(name.startswith('h'))
#True
#endswith()
#判断是否以某个字符或字符串结尾
print(name.endswith('h'))
#False
#判断是否以你好结尾
print(name.endswith('你好'))
#True
#expandtabs()
#把字符串中的 tab 符号('\t')转为空格,tab 符号('\t')默认的空格数是8。
name1 = 'a\tb\tc\td'
print(name1)
#a b c d
print(name1.expandtabs(0))
#abcd
print(name1.expandtabs(4))
#a b c d
print(name1.expandtabs(8))
#a b c d
print(name1.expandtabs(10))
#a b c d
#isalpha()
#判断name是否全是英文字母
print(name.isalpha())
#Flase
print('abc'.isalpha())
#True
当变量是汉字时判断可能有误,需要指定编码格式
a = '哈哈'
print(a.isalpha())
True
print(a.encode('utf-8').isalpha())
False
#isdigit()
#判断是否全是正整数字,只能是正整数,不能有小数点或负数
print('abc123'.isdigit())
#Flase
print('123'.isdigit())
#True
#isalnum()
#判断是否只包含字母和数字,不能包含汉字或特殊字符等
print(name.isalnum())
#False
print('admin123'.isalnum())
#True
#isdecimal()
#判断字符串是否只包含十进制字符
print('admin123'.isdecimal())
#Flase
print('0123'.isdecimal())
#True
#python中str函数isdigit、isdecimal、isnumeric的区别
num1 = '1'
print(num1.isdigit())
#True
print(num1.isdecimal())
#True
print(num1.isnumeric())
#True
num2 = '四'
print(num2.isdigit())
#False
print(num2.isdecimal())
#False
print(num2.isnumeric())
#True
import unicodedata
print([unicodedata.numeric(i) for i in ["〇","零","一","壱","二","弐","三","参","四","五","六","七","八","九","十","廿","卅","卌","百","千","万","亿"]])
#[0.0, 0.0, 1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 20.0, 30.0, 40.0, 100.0, 1000.0, 10000.0, 100000000.0]
#
print([int(unicodedata.numeric(i)) for i in ["〇","零","一","壱","二","弐","三","参","四","五","六","七","八","九","十","廿","卅","卌","百","千","万","亿"]])
#[0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 100, 1000, 10000, 100000000]
#
#isspace()
#判断字符串是否只包含空格
print('1 2 3'.isspace())
#False
print(' '.isspace())
#True
#isidentifier()
#判断是否是一个合法的变量名
print('*'*50)
print('a_1'.isidentifier())
#True
#join()
#使用.将后面的字符串连接起来
print('.'.join('abcd'))
#a.b.c.d
print('|'.join(['a','b','c','d']))
#a|b|c|d
>>> "-".join("GNU/Linux is great".split())
'GNU/Linux-is-great'
#strip(),lstrip(),rstrip()
#将两边的空格和换行符都切除
print(' hello,world\n'.strip())
#hello,world
#只将左边的空格和换行符切除
print(' hello,world\n'.lstrip())
#hello,world
#空行
#只将右边的空格和换行符切除
print(' hello,world\n'.rstrip())
# hello,world
#删除两端的字符串
name = 'abcdeba'
print(name.strip('ab'))
#cde
#translate()
#定义一个明文字符串from_str,和一个加密字符串to_str,定义两个字符串的转换关系,要求2个字符串长度必须相等
#将'abcd345'中存在的可转换字符进行转换,没有转换关系的字符保留
from_str = 'admin123'
to_str = '!@#$%^&*'
trans_table = str.maketrans(from_str,to_str)
print('abcd345'.translate(trans_table))
#!bc@*45
#partition()
#将字符串以最左面的o为中间值进行分隔,分隔成3段
print('helloworld'.partition('o'))
#('hell', 'o', 'world')
#rpartition()
#将字符串以最右面的o为中间值进行分隔,分隔成3段
print('helloworld'.rpartition('o'))
#('hellow', 'o', 'rld')
#replace()
#替换字符,将l替换成L
print('hello,world'.replace('l','L'))
#heLLo,worLd
#替换字符,将l替换成L且只替换1次
print('hello,world'.replace('l','L',1))
#heLlo,world
#split(),splitlines()
#以o为分隔进行切片,o自身会被切除
print('hello\nworld !'.split('o'))
#['hell', '\nw', 'rld !']
#可以指定切片次数
print('hello\nworld !'.split('o',1))
['hell', '\nworld !']
#默认以空格或者换行符进行切片
print('hello\nworld !'.split())
#['hello', 'world', '!']
a = '序号 部门 人数 平均年龄 备注'
print(a.split())
#['序号', '部门', '人数', '平均年龄', '备注'] 有多个空格也可以一次分割
#以换行符进行切片
print('hello\nworld !'.splitlines())
#['hello', 'world !']
#zfill()
#占40个字符,不够的用0补齐
print('heLlo,world'.zfill(40))
#00000000000000000000000000000heLlo,world
swapcase()
#返回字符串大小写交换后的版本
s = "I am A pRoGraMMer"
print(s.swapcase())
#'i AM a PrOgRAmmER'
回文检查
回文是一种无论从左还是从右读都一样的字符序列。比如 “madam”。在这个例子中,我们检查用户输入的字符串是否是回文,并输出结果。
s = input("Please enter a string: ")
z = s[::-1]
if s == z:
print("The string is a palindrome")
else:
print("The string is not a palindrome")
day08-字符串操作的更多相关文章
- day08——文件操作
day08 文件操作: open() :打开 f (文件句柄)= open("文件的路径(文件放的位置)",mode="操作文件的模式",encoding=&q ...
- python学习笔记(字符串操作、字典操作、三级菜单实例)
字符串操作 name = "alex" print(name.capitalize()) #首字母大写 name = "my name is alex" pri ...
- shell编程常用的截取字符串操作
1. 常用的字符串操作 1.1. 替换字符串:$ echo ${var/ /_}#支持正怎表达式 / /表示搜索到第一个替换,// /表示搜索到的结果全部替换. ...
- php字符串操作集锦
web操作, 主要就是对字符文本信息进行处理, 所以, 字符串操作几乎占了很大一部分的php操作.包括 注意strstr 和 strtr的区别? 前者表示字符串查找返回字符串,后者表示字符串中字符替换 ...
- java 字符串操作和日期操作
一.字符串操作 创建字符串 String s2 = new String("Hello World"); String s1 = "Hello World"; ...
- [No000078]Python3 字符串操作
#!/usr/bin/env python3 # -*- coding: utf-8 -*- '''Python 字符串操作 string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分 ...
- Python 字符串操作及string模块使用
python的字符串操作通过2部分的方法函数基本上就可以解决所有的字符串操作需求: python的字符串属性函数 python的string模块 1.字符串属性方法操作: 1.>字符串格式输出对 ...
- C语言字符串操作总结大全
1)字符串操作 strcpy(p, p1) 复制字符串 函数原型strncpy(p, p1, n) 复制指定长度字符串 函数原型strcat(p, p1) 附加字符串 函数原型strn ...
- c# 字符串操作
一.字符串操作 //字符串转数组 string mystring="this is a string" char[] mychars=mystring.ToCharArray(); ...
- C语言字符串操作总结大全(超详细)
本篇文章是对C语言字符串操作进行了详细的总结分析,需要的朋友参考下 1)字符串操作 strcpy(p, p1) 复制字符串 strncpy(p, p1, n) 复制指定长度字符串 strcat( ...
随机推荐
- 显示定位方法,提取中间text 封装成函数的方法
tager='工作台' element=WebDriverWait(self.dr,15,0.1).until( eval("lambda x: x."+'find_element ...
- Ajax传参讲解
客户端和服务器 1.请求:request 2.响应:response 服务器响应事件:onreadystatechange() send() 用于向后台传递参数: Ajax的请求方式 get: ...
- centos6.9发送邮件功能
centos6.9发送邮件功能 第一个里程碑 测试发邮件的功能 系统环境centos 6.9 #配置发邮件 /etc/mail.rc中追加 set bsdcompat set fr ...
- convert 批量文件的格式转换
1.将 a.gif 转为 png 格式 convert a.gif a.png 请注意,convert 命令的基本格式为 convert 源文件 [参数] 目标文件 在上面的命令中,源文件是 a.gi ...
- [UE4]删除动画:Remove from frame 5 to frame 18
从当前帧开始删除到结尾的动画帧
- 虚拟机挂载光盘,同时修改yum源为光盘挂载目录
VMware下挂载光盘并安装文件https://blog.csdn.net/gfd54gd5f46/article/details/53968293 linux修改yum本地源的方法https://w ...
- MySQL操作mysqldump命令详解
--all-databases , -A导出全部数据库. --all-tablespaces , -Y导出全部表空间. --no-tablespaces , -y不导出任何表空间信息. --add-d ...
- CNN卷积层:ReLU函数
卷积层的非线性部分 一.ReLU定义 ReLU:全称 Rectified Linear Units)激活函数 定义 def relu(x): return x if x >0 else 0 #S ...
- Hadoop 2.x常用端口及查看方法
Hadoop集群的各部分一般都会使用到多个端口,有些是daemon之间进行交互之用,有些是用于RPC访问以及HTTP访问.而随着Hadoop周边组件的增多,完全记不住哪个端口对应哪个应用,特收集记录如 ...
- C语言:冒泡排序
void sort(int arr[],int len) { ; ; i<len; i++) { printf("第%d轮:\n", i); // len-i+1:新轮比上轮 ...