Python---基础---str
#capitalize首字母大写,其余小写,返回字符串
------------------------------
s = "i LOVE WangXiaoJing"
print(s.capitalize())
-----------------------------
#title() 将每个单词的首字母变为大写 返回的是字符串
s = "i love wangXIAOJING"
s1 = s.title()
print(s1)
----------------------------------
# upper() 将所有字母变为大写字母 返回的是字符串
s = "I 狗 like dog"
print(s.upper())
------------------------------------
# lower() 将所有字母变为小写字母 返回的是字符串
s = "I 狗 like dog"
print(s.lower())
--------------------------------------
# swapcase() 大小写互换 返回的是字符串
s = "I 狗 like dog"
print(s.swapcase())
-------------------------------------------
# len() 计算字符串长度,不属于字符串的内建函数
# leng统计长度是按照字符个数统计,一个汉字的长度为一
s = "I 狗 like dog"
s1 = "I like dog"
print(len(s))
print(len(s1))
---------------------------------------------
# find() 查找指定字符串,找不到返回-1 第一次找到返回第一次索引值
# index() 查找指定字符串,找不到报错
s = 'asdfghjklasdfghjkl'
s1 = s.find('s', 2)
s2 = s.index('a')
print(s1)
print(s2)
-----------------------------------------------
# count() 计算字符串出现次数 返回整形
s = 'adfsdfasdfasdfsdfsddsss'
print(s.count('s'))
------------------------------------------------
#startswith() 检测是否以指定字母开头 返回布尔值
#endswith() 检测是否 以指定字母结束
s = "I like dog"
print(s.startswith('i'))
print(s.startswith('I'))
print('-'*20)
print(s.endswith('g'))
print(s.endswith('o'))
----------------------------------------------
# isupper() 检测所有字母是否是大写字母 返回的是布尔值
s = "f狗sh"
s1 = "DF狗GH"
print(s.isupper())
print(s1.isupper())
print('='*20)
-----------------------------------------------
# islower 检测所有字母是否是小写字母
s = "f狗sh"
s1 = "DF狗GH"
print(s.islower())
print(s1.islower())
print('='*20)
---------------------------------------------------
#istitle() 检测是否以指定标题显示(每个单词首字母大写)
s = "f狗sh"
s1 = "DF狗GH"
print(s.istitle())
print(s1.istitle())
s2 = "I Like Dog"
print(s2.istitle())
print('='*20)
------------------------------------------------------
# isspace() 检测字符串是否是空字符串
s = ' '
s1 = 'i like '
s2 = ' ' #至少有一个,否则返回False
print(s.isspace())
print(s1.isspace())
print(s2.isspace())
-----------------------------------------------------
#说明:汉字在英文字符包裹中被当作字符处理
s = "I 狗 like dog"
s1 = "I 狗 likedog"
print(s.isalpha())
print(s1.isalpha())
print(s2.isalpha())
--------------------------------------------------------
s = "I 狗 like dog"
s1 = "I 狗 like dog132132"
s2 = "3133"
print(s.isalnum())
print(s1.isalnum())
print(s2.isalnum())
print(s3.isalnum())
# isdecimal()
# isnumeric()
s = '123'
print(s.isdigit())
print(s.isdecimal())
print(s.isnumeric())
s = b'101100'
print(s.isdigit())
print('='*20)
s = '123.2'
print(s.isdigit())
print(s.isdecimal())
print(s.isnumeric())
print('='*20)
s = '三壹百'
print(s.isdigit())
print(s.isdecimal())
print(s.isnumeric())
print('='*20)
s = 'III'
print(s.isdigit())
print(s.isdecimal())
print(s.isnumeric())
s = '日照香炉生紫烟*疑是银河落九天*飞流直下三千尺'
list1 = s.split('*')
print(list1)
s = '日照香炉生紫烟\n疑是银河落九天\n飞流直下三千尺'
print(s.splitlines())
list1 = ['日照香炉生紫烟', '疑是银河落九天', '飞流直下三千尺']
s = '*'.join(list1)
print(s)
s = 'abc'
print(len(s))
print(s.ljust(5) + 'a')
print(s.center(5, '#'))
print(s.rjust(5, '#'))
# lstrip() 去掉左侧指定字符,默认空格
# rstrip() 去掉右侧指定字符,默认空格
print('---'+s.strip()+'--')
print('---'+s+'--')
print(s.lstrip('a'))
print(s.lstrip('b'))
print(s.rstrip('c'))
# lstrip() 去掉左侧指定字符,默认空格
# rstrip() 去掉右侧指定字符,默认空格
print('---'+s.strip()+'--')
print('---'+s+'--')
print(s.lstrip('a'))
print(s.lstrip('b'))
print(s.rstrip('c'))
#translate() 进行字符串替换
s = '今天晚上我吃的是小炒肉,可好吃了'
table = s.maketrans('小炒肉', '大白菜')
print(table)
print(s.translate(table))
table = s.maketrans('小炒肉', '大白菜和粉条')
print(table)
print(s.translate(table))
Python---基础---str的更多相关文章
- python基础--str.split
string = 'This +is -a /string' process = string.split('-') process1 = string.split('-')[-1]#-1和-2可能存 ...
- python基础(str,list,tuple)
python是一门动态解释型的强类型定义语言(先编译后解释) 动态类型语言 动态类型的语言编程时,永远也不用给任何变量指定数据类型,该语言会在你第一次赋值给变量时,在内部将数据类型记录下来 解释型 程 ...
- Python之路3【第一篇】Python基础
本节内容 Python简介 Python安装 第一个Python程序 编程语言的分类 Python简介 1.Python的由来 python的创始人为吉多·范罗苏姆(Guido van Rossum) ...
- 进击的Python【第三章】:Python基础(三)
Python基础(三) 本章内容 集合的概念与操作 文件的操作 函数的特点与用法 参数与局部变量 return返回值的概念 递归的基本含义 函数式编程介绍 高阶函数的概念 一.集合的概念与操作 集合( ...
- python基础之文件读写
python基础之文件读写 本节内容 os模块中文件以及目录的一些方法 文件的操作 目录的操作 1.os模块中文件以及目录的一些方法 python操作文件以及目录可以使用os模块的一些方法如下: 得到 ...
- python基础之dict、set及字符
python基础之dict.set及字符串处理 本节内容 字典介绍及内置方法 集合介绍 字符串处理 1.字典介绍及内置方法 字典是python中唯一的映射类型,采用键值对(key-value)的形式存 ...
- Python基础-字符编码与转码
***了解计算机的底层原理*** Python全栈开发之Python基础-字符编码与转码 需知: 1.在python2默认编码是ASCII, python3里默认是utf-8 2.unicode 分为 ...
- python基础知识理解
一.概述 看了一天的python基础语法,基本对python语法有了一个大概的了解(其实之前断断续续也看过python),学习网址:Python 基础教程.因为之前我学过C++,因此在学习python ...
- .Net程序员之Python基础教程学习----列表和元组 [First Day]
一. 通用序列操作: 其实对于列表,元组 都属于序列化数据,可以通过下表来访问的.下面就来看看序列的基本操作吧. 1.1 索引: 序列中的所有元素的下标是从0开始递增的. 如果索引的长度的是N,那么所 ...
- Python之路【第二篇】:Python基础
参考链接:老师 BLOG : http://www.cnblogs.com/wupeiqi/articles/4906230.html 入门拾遗 一.作用域 只要变量在内存中就能被调用!但是(函数的栈 ...
随机推荐
- Matlab 读取文件夹里所有的文件
(image = dir('D:\gesture\*.*'); % dir是指定文件夹得位置,他与dos下的dir用法相同. 用法有三种: 1. dir 是指工作在当前文件夹里 2. dir name ...
- a daemon 守护进程
w Cron and Crontab usage and exampleshttps://www.pantz.org/software/cron/croninfo.html
- 关于linux中移动目录和到指定目录和移动目录中的数据到指定目录
#这里表示将目录node-v12.13.1-linux-x64移动到/usr/local/中重命名为node,所以node目录可以不存在[root@alone ~]# mv node-v12.13.1 ...
- 测开之路一百零八:bootstrap表格
引入bootstrap和jquery 普通表格 html自带的边框线 bootstrap表格属性 bootstrap表格 边框线 鼠标经过变色 压缩表格,减小密度 自适应屏幕 隔行突出(变色) 表格里 ...
- 工具 - MSF
#ms17- use auxiliary/scanner/smb/smb_ms17_010 - exploit use exploit/windows/smb/ms17_010_eternalblue ...
- sql select as
as 可理解为:用作.当成,作为:一般式重命名列名或者表名.例如有表table, 列 column_1,column_2 你可以写成 select column_1 as 列1,column_2 as ...
- The second curriculum design experiment report in spring 2019
2019年第二次课程设计实验报告 一.实验项目名称 贪吃蛇 二.实验项目功能描述 1.小蛇的移动 玩家可以通过 W A S D控制小蛇的上左下右移动,通过函数改变小蛇部位的位置 2.判断游戏失败 当小 ...
- hdu-2255.奔小康赚大钱(最大权二分匹配)
奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- CodeForces 877E DFS序+线段树
CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...
- require 和 import 详解
前言 JS模块化编程是前端小伙伴们必不可少的知识,下面妹子将于自认为比较清晰的方式列举出来. 1 require 特点: 1.运行时加载 2.拷贝到本页面 3.全部引入 1.1 CommonJS No ...