不做大哥好多年 不做大哥好多年
首页
  • MySQL
  • Redis
  • Elasticsearch
  • Kafka
  • Etcd
  • MongoDB
  • TiDB
  • RabbitMQ
  • 01.GO基础
  • 02.面向对象
  • 03.并发编程
  • 04.常用库
  • 05.数据库操作
  • 06.Beego框架
  • 07.Beego商城
  • 08.GIN框架
  • 09.GIN论坛
  • 10.微服务
  • 01.Python基础
  • 02.Python模块
  • 03.Django
  • 04.Flask
  • 05.SYL
  • 06.Celery
  • 10.微服务
  • 01.Java基础
  • 02.面向对象
  • 03.Java进阶
  • 04.Web基础
  • 05.Spring框架
  • 100.微服务
  • Docker
  • K8S
  • 容器原理
  • Istio
  • 数据结构
  • 算法基础
  • 算法题分类
  • 前置知识
  • PyTorch
  • 01.Python
  • 02.GO
  • 03.Java
  • 04.业务问题
  • 05.关键技术
  • 06.项目常识
  • 10.计算机基础
  • Linux基础
  • Linux高级
  • Nginx
  • KeepAlive
  • ansible
  • zabbix
  • Shell
  • Linux内核

逍遥子

不做大哥好多年
首页
  • MySQL
  • Redis
  • Elasticsearch
  • Kafka
  • Etcd
  • MongoDB
  • TiDB
  • RabbitMQ
  • 01.GO基础
  • 02.面向对象
  • 03.并发编程
  • 04.常用库
  • 05.数据库操作
  • 06.Beego框架
  • 07.Beego商城
  • 08.GIN框架
  • 09.GIN论坛
  • 10.微服务
  • 01.Python基础
  • 02.Python模块
  • 03.Django
  • 04.Flask
  • 05.SYL
  • 06.Celery
  • 10.微服务
  • 01.Java基础
  • 02.面向对象
  • 03.Java进阶
  • 04.Web基础
  • 05.Spring框架
  • 100.微服务
  • Docker
  • K8S
  • 容器原理
  • Istio
  • 数据结构
  • 算法基础
  • 算法题分类
  • 前置知识
  • PyTorch
  • 01.Python
  • 02.GO
  • 03.Java
  • 04.业务问题
  • 05.关键技术
  • 06.项目常识
  • 10.计算机基础
  • Linux基础
  • Linux高级
  • Nginx
  • KeepAlive
  • ansible
  • zabbix
  • Shell
  • Linux内核
  • python基础

    • 01.列表
    • 02.字符串
      • 01.字符串常用方法
        • 1.1 find方法
        • 1.2 join方法
        • 1.3 split方法
        • 1.4 strip
        • 1.5 replace
        • 1.6 首字母大写
        • 1.7 Pinyin 模块,将汉字转换成拼音
      • 02.字符串格式化
        • 2.1 使用百分号(%)字符串格式化
        • 2.2 使用format字符串格式化
    • 03.字典
    • 04.集合
    • 05.进程
    • 06.线程
    • 07.协程
    • 08.select、poll、epoll
    • 09.装饰器
    • 10.生成器和迭代器
    • 11.面向对象
    • 12.深浅拷贝
    • 13.垃圾回收
    • 14.上下文管理
    • 15.网络七层
    • 16.高阶函数
    • 17.次重点
  • python模块

  • django

  • flask

  • SYL

  • Celery

  • 微服务

  • python
  • python基础
xiaonaiqiang
2021-02-11
目录

02.字符串

# 01.字符串常用方法

# 1.1 find方法

  • 作用:find方法可以在一个较长的字符串中查找子串,他返回子串所在位置的最左端索引,如果没有找到则返回-1
a = 'abcdefghijk'
print(a.find('abc'))                         #the result : 0
print(a.find('abc',10,100))                    #the result : 11  指定查找的起始和结束查找位置
1
2
3

# 1.2 join方法

  • 作用:join方法是非常重要的字符串方法,他是split方法的逆方法,用来连接序列中的元素,并且需要被连接的元素都必须是字符串。
a = ['1','2','3']
print('+'.join(a))                                    #the result : 1+2+3
1
2

# 1.3 split方法

  • 作用:这是一个非常重要的字符串,它是join的逆方法,用来将字符串分割成序列
print('1+2+3+4'.split('+'))                            #the result : ['1', '2', '3', '4']
1

# 1.4 strip

  • 作用:strip 方法返回去除首位空格(不包括内部)的字符串
print("   test   test    ".strip())                #the result :“test   test”
1

# 1.5 replace

  • 作用:replace方法返回某字符串所有匹配项均被替换之后得到字符串
print("This is a test".replace('is','is_test'))     #the result : This_test is_test a test
1

# 1.6 首字母大写

>>> s = 'aBdkndfkFFD'
>>> s.capitalize()
'Abdkndfkffd'
1
2
3

# 1.7 Pinyin 模块,将汉字转换成拼音

#! /usr/bin/env python
# -*- coding: utf-8 -*-
from xpinyin import Pinyin

while True:
    p = Pinyin()
    fullname = raw_input('name:').strip()
    fullname = fullname.decode('utf8')
    print fullname
    xin = fullname[0]
    ming = fullname[1:]
    name = ming + '.' + xin
    username = p.get_pinyin(name, '')
    print username
    print username + '@yiducloud.cn'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 02.字符串格式化

# 2.1 使用百分号(%)字符串格式化

num = 100
print("%d to hex is %x" %(num, num))        #100 to hex is 64
print("%d to hex is %#x" %(num, num))       #100 to hex is 0x64
1
2
3

# 2.2 使用format字符串格式化

#1. 位置参数
print("{0} is {1} years old".format("tom", 28))            #tom is 28 years old
print("{} is {} years old".format("tom", 28))             #tom is 28 years old
print("Hi, {0}! {0} is {1} years old".format("tom", 28))      #Hi, tom! tom is 28 years old

#2. 关键字参数
print("{name} is {age} years old".format(name = "tom", age = 28))    #tom is 28 years old

#3. 下标参数
li = ["tom", 28]
print("{0[0]} is {0[1]} years old".format(li))          #tom is 28 years old
1
2
3
4
5
6
7
8
9
10
11
上次更新: 2024/3/13 15:35:10
01.列表
03.字典

← 01.列表 03.字典→

最近更新
01
04.数组双指针排序_子数组
03-25
02
08.动态规划
03-25
03
06.回溯算法
03-25
更多文章>
Theme by Vdoing | Copyright © 2019-2025 逍遥子 技术博客 京ICP备2021005373号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式