python字符串方法学习笔记
# 一、字符串大小写转换
# 字符串首字符大写
print("hello world".capitalize())
# 将字符串变为标题
print("hello WORLD".title())
# 将字符串转为大写
print("hello world".upper())
# 把字符串转为小写
print("HELLO WORLD".lower())
# 翻转字符串中的大小写
print("hello WORLD".swapcase())
# 二、字符串分割
# 以某个元素为分割点,将字符串分为3部分,从左往右找到的第一个元素为分割点
print('helloworld'.partition('o'))
# 以某个元素为分割点,将字符串分为3部分,从右往左找到的第一个元素为分割点
print('helloworld'.rpartition('o'))
# 替换原字符串中的元素,默认全部替换,可以指定替换几个(从左往右数)
print("hello world".replace('o', 'a', 1))
# 以某个元素为分割点,将字符串分割,从左往右分割n次
print("hello world".split('o', 1))
# 以某个元素为分割点,将字符串分割,从右往左分割n次
print("hello world".rsplit('o', 1))
# 按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。
print('hello\nworld'.splitlines(True))
# 三、字符串中查找元素
# 统计某个字符串从索引n到y出现的次数,缺省为在整个字符串中查找
print("hello world".count('o', 7, 10))
# 在索引[n , y)之间查找元素,缺省为[:]返回元素的索引,如果没找到返回-1
print("hello world".find('e'))
print("hello world".find('o', 0, 2))
# 在[n, y)之间找元素的索引值,没找到会报错
print("hello world".index('o'))
print("hello world".index('e', 0, 5))
# 四、字符串判断
# 判断字符串是否以某个元素开始
print('helloworld'.startswith('h'))
# 判断字符串的的从[n,y)的索引之间是否以某个字符结尾,返回值为布尔值
print("hello world".endswith('e', 0, 2))
# 判断是否是只有数字或字母
print('abc123'.isalnum())
# 判断是否只含有字母
print('abc'.isalpha())
# 判断字母是否都是小写
print("Hello".islower())
# 判断字符是不是空格
print(" ".isspace())
# 判断是不是字符串是不是标题(单词首字母是不是大写)
print("Hello World".istitle())
# 在元素之间插入指定字符
# 五、字符串格式化
# 字符串居中,规定字符串的总长度,不够用其他字符补齐,默认是空格
print("hello world".center(20, "#"))
# 把字符串中的\t替换为n个空格
print("hello\tworld".expandtabs(tabsize=20))
print('#'.join("hello world"))
# 规定输出字符的长度,并且左对齐,不足部分用指定字符补齐
print("hello world".ljust(20, "#"))
# 规定输出字符的长度,并且右对齐,不足部分用指定字符补齐
print("hello world".rjust(20, "#"))
# 去除字符串左边的的空格
print(' hello'.lstrip())
# 去除字符串右边的的空格
print('hello '.rstrip())
# 去除字符串两边的的空格
print(' hello '.strip())
# 指定字符串的长度,不够在前面补0
print("123".zfill(5))
# 字符串的拼接
print('hello ' + 'world')
print('hello ' * 3)
python字符串方法学习笔记的更多相关文章
- Requests:Python HTTP Module学习笔记(一)(转)
Requests:Python HTTP Module学习笔记(一) 在学习用python写爬虫的时候用到了Requests这个Http网络库,这个库简单好用并且功能强大,完全可以代替python的标 ...
- Python Built-in Function 学习笔记
Python Built-in Function 学习笔记 1. 匿名函数 1.1 什么是匿名函数 python允许使用lambda来创建一个匿名函数,匿名是因为他不需要以标准的方式来声明,比如def ...
- python字符串方法的简单使用
学习python字符串方法的使用,对书中列举的每种方法都做一个试用,将结果记录,方便以后查询. (1) s.capitalize() ;功能:返回字符串的的副本,并将首字母大写.使用如下: >& ...
- python网络爬虫学习笔记
python网络爬虫学习笔记 By 钟桓 9月 4 2014 更新日期:9月 4 2014 文章文件夹 1. 介绍: 2. 从简单语句中開始: 3. 传送数据给server 4. HTTP头-描写叙述 ...
- Python 字符串方法详解
Python 字符串方法详解 本文最初发表于赖勇浩(恋花蝶)的博客(http://blog.csdn.net/lanphaday),如蒙转载,敬请保留全文完整,切勿去除本声明和作者信息. ...
- python 字符串方法整理
Python字符串方法 1.大小写转换 1.1 lower.upper lower():小写 upper():大写 1.2 title.capitalize S.title():字符串中所有单词首字母 ...
- python中strip()方法学习笔记
Python strip() 方法用于移除字符串头尾指定的字符(默认为空格). 当使用strip('xxx'),只要字符串头尾有"xxx"中的一个,就会去掉,而不是符合字符串''x ...
- 【Python】PYTHON中STRIP()方法学习笔记
Python strip() 方法用于移除字符串头尾指定的字符(默认为空格). 当使用strip('xxx'),只要字符串头尾有"xxx"中的一个,就会去掉,而不是符合字符串''x ...
- python网络爬虫学习笔记(二)BeautifulSoup库
Beautiful Soup库也称为beautiful4库.bs4库,它可用于解析HTML/XML,并将所有文件.字符串转换为'utf-8'编码.HTML/XML文档是与“标签树一一对应的.具体地说, ...
随机推荐
- A Tutorial on Using the ALSA Audio API
A Tutorial on Using the ALSA Audio API This document attempts to provide an introduction to the ALSA ...
- 【转载】Spring bean 中 constructor-arg属性
转载地址:https://blog.csdn.net/qq_27292113/article/details/78063696 方便以后查阅
- smbrun - smbd和外部程序间的接口程序。
总览 SYNOPSIS smbrun shell-command 描述 DESCRIPTION 此程序是samba套件的一部分. smbrun是个非常小的“粘合”程序,用于为smbd守护程序smbd( ...
- 【LeetCode】线段树 segment-tree(共9题)+ 树状数组 binary-indexed-tree(共5题)
第一部分---线段树:https://leetcode.com/tag/segment-tree/ [218]The Skyline Problem [307]Range Sum Query - Mu ...
- 同步mysql
ElasticSearch同步MySql 标签: elasticsearchmysql 2016-07-01 09:07 4636人阅读 评论(8) 收藏 举报 分类: Elasticsearch( ...
- c++ 递归思想 阶乘
#include "stdio.h" #include "iostream" long fact(int n); int main() { int i; sca ...
- C++ 穷举算法 鸡兔同笼
#include "stdio.h" int qiongju(int head, int foot, int *chicken, int *rabbit) { int re, i, ...
- 日志数据如何同步到MaxCompute
摘要:日常工作中,企业需要将通过ECS.容器.移动端.开源软件.网站服务.JS等接入的实时日志数据进行应用开发.包括对日志实时查询与分析.采集与消费.数据清洗与流计算.数据仓库对接等场景.本次分享主要 ...
- SPOJ 7258 (后缀自动机)
转载:http://hzwer.com/4492.html 给一个长度不超过90000的串S,每次询问它的所有不同子串中,字典序第K小的,询问不超过500个. 搞出后缀自动机 dp处理出每个点往下走能 ...
- kafka-server.properties
# Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreement ...