python笔记之str常用方法
#Auther Bob
#--*--conding:utf-8 --*--
# s1 = 'aBcdE1d'
# =========================================================================================
# str.capitalize() 把s1这个字符才的大写字母变为小写字母,小写字母变为大写字母
# print(s1.capitalize())
# Abcded # =========================================================================================
# str.casefold() #把字符串s1中的所有字母变为小写字母
# print(s1.casefold())
# abcde1d #=========================================================================================
# str.center() 让字符串的占的长度为20,且把字符串s1放在中间,如果字符串s1不够20个字符,则用'-'号来代替
# print(s1.center(20,'-'))
# ------aBcdE1d------- # ===========================================================================================
# str.count(), 统计一个字符串中某个字符出现的次数
# s2 = "12AAbbCCCCC"
# print(s2.count('1'))
# print(s2.count('C'))
# print(s2.count('c'))
# 1
# 5
# 0
# ===========================================================================================
# str.endswith(), 检查某个字符串是否以某个或者某几个字符串结尾
# s2 = 'aAAABbcde'
# print(s2.endswith('e'))
# print(s2.endswith('cde'))
# print(s2.endswith('E')) # True
# True
# False
# =============================================================================================
# str.find() 判断字符串中是否有某个或者某几个字符,如果有则返回该字符的索引,如果没有则返回-1
# print(s2.find('A'))
# print(s2.find('F')) # 1
# -1
#==============================================================================================
# str.index() 查看字符串中某个字符或者某几个字符的的索引,且只会显示第一个字符的索引
# s2 = 'aAAABbcde'
# print(s2.index('a'))
# print(s2.index('A'))
# 0
# 1 #==============================================================================================
# str.isalnum() 判断字符串中是否均为字母和数字,如果是则返回为True,如果不是则返回False
# s1 = 'aBcdE1d'
# s2 = 'ad_+1'
# print(s1.isalnum())
# print(s2.isalnum()) # True
# False #==============================================================================================
# str.isalpha() 判断字符串是否全部为字母,如果是则为True,如果不是,则为False
# s1 = 'aBcdEd'
# s2 = '123'
# print(s1.isalpha())
# print(s2.isalpha()) # True
# False #==============================================================================================
# str.isdecimal() #==============================================================================================
# str.isdigit() 判断字符串是否全部为数字,如果是则返回True,如果不是,则返回False
# s1 = '1aBcdEd'
# s2 = '123'
# print(s1.isdigit())
# print(s2.isdigit()) # False
# True #==============================================================================================
# str.istitle() 判断字符串中的每个单词的首字母是否大写,如果是则返回True,如果不是,则返回False
# s1 = 'A Bod Stuedent Is'
# s2 = 'A bdd sde Is'
# print(s1.istitle())
# print(s2.istitle()) # True
# False #==============================================================================================
# str.join() 用来连接序列中的字符串
# l1 = ['a','b','c']
# t1 = ('d','e','f')
# s1 = {'g','h','i'}
# print(str.join('-',l1))
# print(str.join('+',t1))
# print(str.join('*',s1)) # a-b-c
# d+e+f
# h*g*i #==============================================================================================
# str.isupper() 判断字符串中是否全部为大写,如果是则返回True,如果不是则返回False
# s1 = 'AAAAAA'
# s2 = 'aA'
# print(s1.isupper())
# print(s2.isupper())
# True
# False
#==============================================================================================
# str.title() 把字符串的每个单词的首字母全部大写,并赋值给另外一个变量 # s1 = 'a sde aedd edeadf'
# ret = s1.title()
# print(ret) # A Sde Aedd Edeadf
#==============================================================================================
# str.split() 把一个字符串按照某个字符分割,然后把分割后的每个字符串放在一个list中
# s1 = 'a/b/c'
# s2 = 'a1cbda1cd'
# print(s1.split('/'))
# print(s2.split('1'))
# ['a', 'b', 'c']
# ['a', 'cbda', 'cd'] #=============================================================================================
# str.partition() 是根据某个字符把字符串分为三份,然后放在一个tuple中
# s1 = 'a/b/c'
# s2 = 'a1cbda1cd'
# print(s1.partition('/'))
# print(s2.partition('1')) # ('a', '/', 'b/c')
# ('a', '1', 'cbda1cd') #==============================================================================================
# str.strip() 如果strip不加任何参数,则会把字符串两边的空格全部去掉,也可以指定参数,去掉字符串两边的指定的字符
# s1 = ' adb '
# print(s1)
# print(s1.strip()) # adb
# adb # s2 = 'aaBBBccccAAAaaaa'
# print(s2)
# print(s2.strip('a')) # aaBBBccccAAAaaaa
# BBBccccAAA
#==============================================================================================
# str.startswith() 判断字符串是否以某个或者某几个字符开始
# s1 = 'aaaacccccc'
# print(s1.startswith('a'))
# print(s1.startswith('c')) # True
# False #==============================================================================================
# str.upper() 把字符串全大写,并赋值给另外一个变量
# s1 = 'abcde'
# ret = s1.upper()
# print(ret) # ABCDE
#==============================================================================================
# str.ljust() 用来填充字符串的,在字符串的右边添加a,直到新的字符串的长度到10个字符为止,并赋值给另外一个变量
# s1 = 'abcde'
# ret = s1.ljust(10,'a')
# print(ret) # abcdeaaaaa
#==============================================================================================
# str.rjust() 用来填充字符串的,在字符串的左边添加a,直到新的字符串的长度到10个字符为止,并赋值给另外一个变量
# s1 = 'addedd'
# ret = s1.rjust(20,'&')
# print(ret) # &&&&&&&&&&&&&&addedd #==============================================================================================
替换字符串中的单个字符或者字符串
ret = name1.replace('C','E')
print(ret)
ret = name1.replace('Ce','EF')
print(ret)
#==============================================================================================
判断两个字符串是否相等
name1 = 'Cevin'
name2 = 'Cevin bob Cen'
if name1.__eq__(name2):
print("name1 is equal name2")
#==============================================================================================
大写变为小写,小写变为大写
ret = name.swapcase()
print(ret)
#==============================================================================================
填充,把tab【\t】建替换为空格
name = "Cev\ti\tn"
ret = name.expandtabs()
print(ret)
python笔记之str常用方法的更多相关文章
- Python基础之str常用方法、for循环
初学python,有些地方可能还不够明白,希望各位看官发现我的错误后留言指正! 一.字符串的索引与切片 注:字符串的第一位的索引值是0 1.索引案例 s = 'abcd' s1 = s[0] prin ...
- Python:笔记(6)——正则表达式
Python:笔记(6)——正则表达式 re模块 re模块用于在字符串中执行基于正则表达式模式的匹配和替换. 使用原始字符串 正则表达式使用 \ 对特殊字符进行转义,比如,为了匹配字符串 ‘pytho ...
- Python笔记之不可不练
如果您已经有了一定的Python编程基础,那么本文就是为您的编程能力锦上添花,如果您刚刚开始对Python有一点点兴趣,不怕,Python的重点基础知识已经总结在博文<Python笔记之不可不知 ...
- python笔记 - day8
python笔记 - day8 参考: http://www.cnblogs.com/wupeiqi/p/4766801.html http://www.cnblogs.com/wupeiqi/art ...
- python笔记 - day7
python笔记 - day7 参考: http://www.cnblogs.com/wupeiqi/articles/5501365.html 面向对象,初级篇: http://www.cnblog ...
- python笔记 - day5
python笔记 - day5 参考: http://www.cnblogs.com/wupeiqi/articles/5484747.html http://www.cnblogs.com/alex ...
- python笔记 - day3
python笔记 - day3 参考:http://www.cnblogs.com/wupeiqi/articles/5453708.html set特性: 1.无序 2.不重复 3.可嵌套 函数: ...
- python笔记之编程风格大比拼
python笔记之编程风格大比拼 虽然我的python age并不高,但我仍然愿意将我遇到的或者我写的有趣的python程序和大家一块分享,下面是我找到的一篇关于各类python程序员的编程风格的比较 ...
- python笔记之常用模块用法分析
python笔记之常用模块用法分析 内置模块(不用import就可以直接使用) 常用内置函数 help(obj) 在线帮助, obj可是任何类型 callable(obj) 查看一个obj是不是可以像 ...
随机推荐
- Linux 期中架构 SSH
为其他网络服务提供安全协议.替代Telnet SSH: 实现数据加密传输 22 默认支持root用户远程连接 类似sftp-server服务 nmap -p 22 10.0.0.41 nc 10 ...
- xshell配置通过堡垒机直接登陆到内网机器
在xshell中文件-->新建菜单,打开新建会话属性,填写堡垒机的IP端口和账号密码后,进入登录脚本 : 勾选"执行以下的期望和发送组合对(X) " (1)添加: 期望: 发 ...
- SDL播放音频的时候发现SDL_OpenAudioDevice打开一直失败
1:在使用SDL播放音频的时候发现SDL_OpenAudioDevice打开一直失败,导致SDL不能进入回调函数. 使用SDL_GetError()打印错误提示XAudio2: XAudio2Crea ...
- css border-bottom(指定下边线的样式、宽度及颜色)
border-bottom(指定下边线的样式.宽度及颜色) border-bottom: 值: border-bottom-style:值; border-bottom-color: 值; borde ...
- 开发组件:ZeroMQ
ZeroMQ https://blog.csdn.net/w174504744/article/details/73187697
- 目前学习的爬取小数据图片zzz
import os import threading import re import time from lxml import etree all_img_urls = [] # 图片列表页面的数 ...
- 使border处于边框内
box-sizing需要指定高度,它在这个高度出现,不会增加额外的高度 .box{box-sizing: border-box;height: 64px;}
- c++官方文档
来自官方文档...感谢老王指出需要c++11,一下代码全在c++11下编译,编译参数加入 -std=c++11 #include<stdio.h> #include<iostrea ...
- python2-python3字符串
https://www.cnblogs.com/yangmingxianshen/p/7990102.html
- quartz 的简单使用
0.依赖: <!-- 引入quartz对应的依赖 --> <dependency> <groupId>org.quartz-scheduler</groupI ...