python字符串常用方法
#1、strip()去掉空格(字符串首、尾空格)、lstrip()去掉左侧空格、rstrip()去掉右侧空格
print(' abc '.lstrip())
#>>abc
print(' abc '.rstrip())
#>> abc
print(' abc '.strip())
#>>abc #2、split 切割,splitlines按照换行符分割 注:结果为列表
print('a|b|c'.split('|'))
#>>['a', 'b', 'c']
print('1+2+3\n1+2+3+4'.splitlines()) # 按照换行符分割
#>>['1+2+3', '1+2+3+4'] split与os.path.split()对比
|
Split 字符串 |
拆分字符串。返回字符串列表。 |
|
|
1、默认情况下,使用空格作为分隔符,则分隔后,空串会自动忽略 |
s = 'love python' |
|
|
2、若显示指定空格做分隔符,则不会忽略空串 |
s = 'love python' |
|
|
3、指定分隔次数 |
s = 'www.pku.edu.cn' |
|
|
4、分隔后直接存入变量 (分隔1次,并把分隔后的2个字符串存放在s1和s2中) |
s1,s2=s.split('.',1) |
|
|
os.path.split() |
返回元组类型 |
|
|
将文件名和路径分割开 |
import os |
|
#3、upper() 所有字母大写,lowwer()所有字母小写,swapcase() 大小写反转
print('abc'.upper())
#>>ABC
print('ABC'.lower())
#>>abc
print('Abc'.swapcase())
#>>aBC #4、replace(‘原字符’,‘新字符’)
print('hello cat'.replace('hello','hi'))
#>>hi cat #5、找位置序列号
# index() 按照指定字符返回索引号 注:如果不存在将报错
# find() 按照指定字符返回索引号 注:如果不存在不报错,返回-1
print('abc'.index('a'))
#>>0
print('abc'.find('t'))
#>>-1 #6、count() 查找某个字符总个数
print('abcattv'.count('t'))
#>>2 #7、切片
# 注意形式':' 结果左闭右开
print('abcdef'[0:5])
#>>abcde #8、填充0 zfill(n) 在字符串前加0,字符总数n
print('abc'.zfill(5))
#>>00abc #9、放中间位置 (字符总数,补充的字符)
print('abc'.center(20,'t'))
#>>ttttttttabcttttttttt #10、格式化
# a:用%s占位 注:不带'.'
print('a%sc'%('ttt'))
#>>atttc
# b:format 用{}占位
print('a{}c'.format('b'))
#>>abc
print('{}+{}={}'.format(1, 2, 1 + 2))
#>>1+2=3
#c:format_map 用{}占位 format_map后接字典形式
print('a{t1}c{t2}'.format_map({'t1':'b','t2':'d'}))
#>>abcd
#d:f用{}占位 -- python3.6以上支持
print(f"a{'b'}c")
#>>abc
#11、字符串连接 join内容为字符串、列表形式
print('123'.join('deg'))
#>>d123e123g
print('+'.join('abc'))
#>>a+b+c
#12、expandtabs(通常可用于表格格式的输出),返回字符串中的 tab 符号('\t')转为空格后生成的新字符串
info ="name\tage\temail\nlily\t22\t123@qq.com\njames\t33\t456@qq.com"
print(info.expandtabs(10))
#>>
'''
name age email
lily 22 123@qq.com
james 33 456@qq.com
'''
#13、import string
import string
#0-9之间整数集
string.digits
# 特殊字符集
string.punctuation
# 小写字母集
string.ascii_lowercase
# 大写字母集
string.ascii_uppercase
#------------------返回布尔类型--------------------------- #1、是否以某字符开头、结尾----返回true与false
print('ttt'.startswith('t'))
#>>True
print('ttt'.endswith('t'))
#>>True
#2、是否是数字与字母
# 是否是数字
print('3335'.isdigit())
#>>True
#是否全部是字母或者数字
print('aaa11'.isalnum())
#>>True
#是否全部是字母
print('1'.isalpha())
#>>False
#3、是否是一个合法的变量名
print('aa'.isidentifier()) # 是否是一个合法的变量名
#4、判断是否是大小写
print('aa'.islower()) # 是否是小写字母
print('AA'.isupper()) # 是否是大写字母 #5、判断首字母是否大写
print('Toadrunner Book'.istitle()) # 是不是一个标题,判断首字母是否大写
#>>True
print('Toadrunner book'.istitle())
#>>False
python字符串常用方法的更多相关文章
- python 字符串常用方法
字符串常用方法 capitalize() String.capitalize() 将字符串首字母变为大写 name = 'xiaoming' new_name = name.capitalize() ...
- Python字符串常用方法(二)
二.字符串的操作常用方法 字符串的替换.删除.截取.复制.连接.比较.查找.分割等 1. string. lower() :转小写 2. string. upper() :转大写 3. string. ...
- python字符串常用方法、分割字符串等
一.字符串的常用方法 1.str.capitalize() 字符串首字母大写 2.str.center() 把字符串居中 3.str.isalnum() 判断字符串是否含有英文.数字,若有英文和数 ...
- Python字符串常用方法(一)
一.字符串的判断常用方法 字符串的字母,数字,大小写,空格等的判断 1.string. isalnum() :(字母数字判断) 如果 string 至少有一个字符并且所有字符都是字母或数字则返回 Tr ...
- Python 字符串常用方法总结
明确:对字符串的操作方法都不会改变原来字符串的值 1,去掉空格和特殊符号 name.strip() 去掉空格和换行符 name.strip('xx') 去掉某个字符串 name.lstrip() ...
- Python 字符串常用方法 day2
1.去空格和换行符: s = ' a bc ' print(s.strip())#strip(),去掉字符串两边的空格和换行符,无法去除中间的空格 print(s.rstrip())#rstrip() ...
- python 字符串 常用方法
name = 'ALLix9' print(name.casefold()) # 大写变成小写 name.lower() # 全变小写 '.isnumeric()) #判断是否是数字:正整数 prin ...
- python基础(2)字符串常用方法
python字符串常用方法 find(sub[, start[, end]]) 在索引start和end之间查找字符串sub 找到,则返回最左端的索引值,未找到,则返回-1 start和end都可 ...
- 【python基础语法】字符串常用方法 、列表(第3天课堂笔记)
""" 字符串的方法 join 字符串拼接,将列表转换为字符串 find 查找元素位置 count 查找元素个数 replace 替换字符 split 字符串分割,将字符 ...
随机推荐
- 各种软件的安装教程centos mysql tomcat nginx jenkins jira 等等
464 Star3,606 Fork 1,460 judasn/Linux-Tutorial 作者: https://github.com/judasn Linux-Tutorial/markdow ...
- SNF快速开发平台--规则引擎整体介绍及使用说明书
一.设计目标 a)规则引擎语法能够满足分单,计费,WMS策略的配置要求.语法是一致和统一的 b)能够在不修改规则引擎模块的情况下,加入任意一个新的规则:实现上述需求之外的规则配置需求 c)运算速度快 ...
- 服务端怎样暴露IBinder接口对象
服务端怎样暴露IBinder接口对象: package com.example.mydownload; import android.app.Service; import android.conte ...
- [svc]linux上vxlan实战
linux vxlan实现2台机器的通往段ip互通 - 在n1上 ip l a vxlan0 type vxlan id 42 dstport 4789 remote 192.168.1.12 loc ...
- [docker]使用quaaga实现(rip ospf)实现主机间容器互通
使用quaaga实现(rip ospf)实现主机间容器互通 - n1设置 brctl addbr br0 ip a a 10.1.1.1/24 br0 ip a a 10.1.1.1/24 dev b ...
- [转]深刻理解Python中的元类(metaclass)以及元类实现单例模式
使用元类 深刻理解Python中的元类(metaclass)以及元类实现单例模式 在看一些框架源代码的过程中碰到很多元类的实例,看起来很吃力很晦涩:在看python cookbook中关于元类创建单例 ...
- GNU Binutils简介及基本用法
[时间:2017-06] [状态:Open] [关键词:GNU, binutils, as, ld, ar, 基础工具,linux,链接器,汇编器] 0 简介 GNU Binary Utilities ...
- [转]css实现左侧宽度自适应,右侧固定宽度
原文地址:https://segmentfault.com/a/1190000008411418 页面布局中经常用会遇到左侧宽度自适应,右侧固定宽度,或者左侧宽度固定,右侧自适应.总之就是一边固定宽度 ...
- 线程安全的ConcurrentQueue<T>队列
队列(Queue)代表了一个先进先出的对象集合.当您需要对各项进行先进先出的访问时,则使用队列.当您在列表中添加一项,称为入队,当您从列表中移除一项时,称为出队. ConcurrentQueue< ...
- WebRTC 零基础开发者教程(中文版)下载
WebRTC 简介 WebRTC,是一个支持网页浏览器进行实时语音通话或视频聊天的技术,是谷歌2010年以6820万美元收购Global IP Solutions公司而获得的一项技术. WebRTC提 ...