字符串的常见操作

"""

DATA STRUCTURE

Container: Sequence

—— String

String is immutable.If string transfer to List, it can be mutable.

Another way to change the content of Strings, use inner API, such as replace(),

translate(), find(), join(), split().

数据结构

容器:序列

—— 字符串

字符串是不可变的。如果将之转换成列表,则可变。

另一种改变字符串的方式,使用字符串方法,诸如 replace(), translate(), find(), join(), split().

"""


字符串方法

def string_api():

    words = "这是一段文字,包括了一些符合对如()[],也有一些特殊符号!@#$"

    print(words.title())    # 打印结果为:这是一段文字,包括了一些符合对如()[],也有一些特殊符号!@#$
print('/'.join(words)) # 打印结果为:这/是/一/段/文/字/,/包/括/了/一/些/符/合/对/如/(/)/[/]/,/也/有/一/些/特/殊/符/号/!/@/#/$
print(words.split(',', 2)) # 打印结果为:['这是一段文字', '包括了一些符合对如()[],也有一些特殊符号!@#$']
print(words.replace("是", "展示出了")) # 打印结果为:这展示出了一段文字,包括了一些符合对如()[],也有一些特殊符号!@#$
print(words.find('!')) # 打印结果为:29
print(words[:5]) #打印结果为:这是一段文

格式化字符串方式

详细可见 https://docs.python.org/zh-cn/3.7/tutorial/introduction.html#strings

#! /usr/bin/python
# coding:utf-8 from math import pi class StringFormat: @staticmethod
def string_format():
string = 'Hey, %s. %s enough for ya?'
values = ('guys', 'Hot')
# This is simple-format
# 简单字符串格式化方法
print(string % values)
# This is standard format
# 标准字符串格式化方法
string_d = 'Hello, {1}. {0} enough for ya?'.format("Hot", "guys")
print(string_d)
# This is for remaining 2 decimals
# 保留2位数
print("{name} is approximately {value:.2f}.".format(value=pi, name="π"))
# transfer symbol
# 转换标识符
print("{pi!s} {pi!r} {pi!a}".format(pi="π")) @staticmethod
def string_sub(string='Hello'):
if string.find('o') != -1:
print('find one character:o')
print('the first index of substring is:' + str(string.find('o')) + " position")
else:
print("nothing") if __name__ == '__main__':
StringFormat.string_format()
StringFormat.string_sub()

字符串模板方式

#! /usr/bin/python
# coding:utf-8 from string import Template def tmplt_action():
s1 = Template('$who like $what')
print(s1.substitute(who='tim', what='eat')) tmplt_action() # 输出结果为:tim like eat

[每日一讲] Python系列:字符串(下)的更多相关文章

  1. [每日一讲] Python系列:字符串(上)

    字符串作为人类最常处理的内容,在计算中决定了其占有重要的地位.在 Python 中,字符串的操作和处理往往需要根据实际问题,结合其他操作才可以完成目标.在复杂世界仅仅是字符串 API 还无法完成工作. ...

  2. [每日一讲] Python系列:列表与元组

    参考文档 https://docs.python.org/zh-cn/3.7/tutorial/introduction.html#lists """ DATA STRU ...

  3. [每日一讲] Python系列:数字与运算符

    数字(数值)型 Python 数字数据类型用于存储数值.数据类型是不可变(immutable)的,这就意味着如果改变数字数据类型的值,将重新分配内存空间. Python 支持三种不同的数值类型: 整型 ...

  4. [每日一讲] Python系列:Python概述

    Python 序章 概述 Python 是弱类型动态解释型的面向对象高级语言,其具备面向对象的三大特点:封装.继承.多态.Python 代码运行时,其有一个编译过程,通过编译器生成 .pyc 字节码 ...

  5. [每日一讲] Python系列:字典

    #! /usr/bin/python # coding:utf-8 """ DATA STRUCTURE Container: Mapping (Another cont ...

  6. python学习第九讲,python中的数据类型,字符串的使用与介绍

    目录 python学习第九讲,python中的数据类型,字符串的使用与介绍 一丶字符串 1.字符串的定义 2.字符串的常见操作 3.字符串操作 len count index操作 4.判断空白字符,判 ...

  7. python系列四:Python3字符串

    #!/usr/bin/python #Python3 字符串#可以截取字符串的一部分并与其他字段拼接var1 = 'Hello World!'print ("已更新字符串 : ", ...

  8. Python中字符串使用单引号、双引号标识和三引号标识,什么是三引号?什么情况下用哪种标识?

    一.三引号是指三个单引号或者三个双引号: 二.Python中字符串如果以单引号.双引号标识和三引号标识开头,则字符串结尾也必须是对应的标识,不能变更: 三.三者的异同: 1.三者都是字符串,大部分情况 ...

  9. 【Python】 字符串的相关小知识点

    字符串 很久以前,刚接触IT知识的时候,我觉得字符串还有字符变量是很奇葩的存在.数字,数组,字典什么的这些数据类型要不就是自然界固有的要不就是为了计算方便而发明出来的一些数据的组合方式.但是字符串这玩 ...

随机推荐

  1. zabbix使用脚本监控

    参照:http://www.cnblogs.com/zhongkai-27/p/9984597.html

  2. Kafka集群搭建和配置

    Kafka配置优化 https://www.jianshu.com/p/f62099d174d9 1.安装&配置 下载tar包 解压后即可使用 修改配置文件 将server.propertie ...

  3. linux 编程头文件搜索规则

    包含头文件有两种写法,分别是:#include <stdio.h>#include "stdio.h" <>和""分别表示搜索位置的方式 ...

  4. 从零开始学习GDI+ (三) 画笔与画刷

  5. spark日志+hivesql

    windows本地读取hive,需要在resource里面将集群中的hive-site.xml下载下来. <?xml version="1.0" encoding=" ...

  6. jqery实现10X10的表格,双击消失

    <script type="text/javascript"> $(document).ready(function(){ //循环拼接html s="&qu ...

  7. Chrome之谷歌插件开发

    最近碰到一个需求,需要在某个平台上批量的添加好友,如果是人工点击,可以操作,但是效率并不高,人工成本较高.就打算使用浏览器插件的方式来完成这件重复性的工作. 介绍: Chrome插件的本质就是一个由  ...

  8. git 查看对比的方法log diff

    git shortlog 默认情况下,git shortlog 把输出按作者名字排序,但你可以传入 -n 选项来按每个作者提交数量排序. 1.有冲突时可以用 git status查看 2.通过git ...

  9. jq each遍历数组或对象

    var arr = ["北京","上海","天津","重庆","河北","河南" ...

  10. redis删除主从节点

    1.删除一个Slave节点 ./redis-cli --cluster del-node 127.0.0.1:7001 74957282ffa94c828925c4f7026baac04a67e291 ...