#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'
print(s.split())
#>>>>>['love', 'python']

2、若显示指定空格做分隔符,则不会忽略空串

s = 'love    python'
print(s.split(' '))
#>>>>>['love', '\xa0', '\xa0python']

3、指定分隔次数

s = 'www.pku.edu.cn'
print(s.split('.',0))
#>>>>>['www.pku.edu.cn']
print(s.split('.',1))
#>>>>>['www', 'pku.edu.cn']
print(s.split('.',-1)) #尽可能多的分隔,与不加num参数相同
#>>>>>['www', 'pku', 'edu', 'cn']

4、分隔后直接存入变量

(分隔1次,并把分隔后的2个字符串存放在s1和s2中)

s1,s2=s.split('.',1)
print(s1)
print(s2)
#>>>>>www
#>>>>>pku.edu.cn

os.path.split()

返回元组类型

将文件名和路径分割开

import os
t=os.path.split('C:/soft/python/test.py') #将文件名和路径分割开
print(t)
#>>>>>('C:/soft/python', 'test.py')


#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字符串常用方法的更多相关文章

  1. python 字符串常用方法

    字符串常用方法 capitalize() String.capitalize() 将字符串首字母变为大写 name = 'xiaoming' new_name = name.capitalize() ...

  2. Python字符串常用方法(二)

    二.字符串的操作常用方法 字符串的替换.删除.截取.复制.连接.比较.查找.分割等 1. string. lower() :转小写 2. string. upper() :转大写 3. string. ...

  3. python字符串常用方法、分割字符串等

    一.字符串的常用方法 1.str.capitalize()  字符串首字母大写 2.str.center()  把字符串居中 3.str.isalnum() 判断字符串是否含有英文.数字,若有英文和数 ...

  4. Python字符串常用方法(一)

    一.字符串的判断常用方法 字符串的字母,数字,大小写,空格等的判断 1.string. isalnum() :(字母数字判断) 如果 string 至少有一个字符并且所有字符都是字母或数字则返回 Tr ...

  5. Python 字符串常用方法总结

    明确:对字符串的操作方法都不会改变原来字符串的值 1,去掉空格和特殊符号 name.strip()  去掉空格和换行符 name.strip('xx')  去掉某个字符串 name.lstrip()  ...

  6. Python 字符串常用方法 day2

    1.去空格和换行符: s = ' a bc ' print(s.strip())#strip(),去掉字符串两边的空格和换行符,无法去除中间的空格 print(s.rstrip())#rstrip() ...

  7. python 字符串 常用方法

    name = 'ALLix9' print(name.casefold()) # 大写变成小写 name.lower() # 全变小写 '.isnumeric()) #判断是否是数字:正整数 prin ...

  8. python基础(2)字符串常用方法

    python字符串常用方法 find(sub[, start[, end]]) 在索引start和end之间查找字符串sub ​找到,则返回最左端的索引值,未找到,则返回-1 ​start和end都可 ...

  9. 【python基础语法】字符串常用方法 、列表(第3天课堂笔记)

    """ 字符串的方法 join 字符串拼接,将列表转换为字符串 find 查找元素位置 count 查找元素个数 replace 替换字符 split 字符串分割,将字符 ...

随机推荐

  1. Innotop的安装和使用

    功能特点1.显示当前innodb的全部事务列表:2.显示当前正运行着的查询:3.显示当前锁和锁等等的列表:4.服务器状态和变量的摘要信息 显示了数值的相对变化幅度:5.有多种模式可用来显示Innodb ...

  2. C++ 匿名namespace的作用以及与static的区别

    匿名namespace的作用以及它与static的区别 一.匿名namespace的作用 在C语言中,如果我们在多个tu(translation unit)中使用了同一个名字做 为函数名或者全局变量名 ...

  3. Nginx负载均衡权重,ip_hash

    nginx为后端web服务器(apache,nginx,tomcat,weblogic)等做反向代理 几台后端web服务器需要考虑文件共享,数据库共享,session共享问题.文件共享可以使用nfs, ...

  4. Android开发(十五)——ListView中Items的间距margin

    ListView中Items没有margin 参考:http://www.cnblogs.com/xitang/p/3677528.html

  5. sqlmap tamter

    支持的数据库 编号 脚本名称 作用 实现方式 all 1 apostrophemask.py 用utf8代替引号 ("1 AND '1'='1") '1 AND %EF%BC%87 ...

  6. mongodb批量操作, bulk_write,

    需要批量操作时候,节省网络连接交互次数,可以使用 bulk_write. 设置ordered=False,因为批量操作中没有互相依赖关系,如果有前后顺序的互相依赖,需要设置为True. bed_typ ...

  7. 【hadoop】 hdfs shell 命令交互

    1.put 本地文件上传至hdfs中 2. cat 查看内容 3. 删除文件,文件夹 4. ls 5. copyFromLocal 复制本地文件到HDFS , copyToLocal hdfs 复制到 ...

  8. MTK framework系统默认设置

    Android 5.1 最新framework系统默认设置 一般默认位置:frameworks\base\packages\SettingsProvider\res\values\defaults.x ...

  9. [DQN] What is Deep Reinforcement Learning

    已经成为DL中专门的一派,高大上的样子 Intro: MIT 6.S191 Lecture 6: Deep Reinforcement Learning Course: CS 294: Deep Re ...

  10. [IR] XML Compression

    Ref: https://www.ibm.com/developerworks/cn/xml/x-datacompression/ Language-Equivalent (类似路径压缩 ) root ...