[每日一讲] Python系列:字符串(下)
字符串的常见操作
"""
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系列:字符串(下)的更多相关文章
- [每日一讲] Python系列:字符串(上)
字符串作为人类最常处理的内容,在计算中决定了其占有重要的地位.在 Python 中,字符串的操作和处理往往需要根据实际问题,结合其他操作才可以完成目标.在复杂世界仅仅是字符串 API 还无法完成工作. ...
- [每日一讲] Python系列:列表与元组
参考文档 https://docs.python.org/zh-cn/3.7/tutorial/introduction.html#lists """ DATA STRU ...
- [每日一讲] Python系列:数字与运算符
数字(数值)型 Python 数字数据类型用于存储数值.数据类型是不可变(immutable)的,这就意味着如果改变数字数据类型的值,将重新分配内存空间. Python 支持三种不同的数值类型: 整型 ...
- [每日一讲] Python系列:Python概述
Python 序章 概述 Python 是弱类型动态解释型的面向对象高级语言,其具备面向对象的三大特点:封装.继承.多态.Python 代码运行时,其有一个编译过程,通过编译器生成 .pyc 字节码 ...
- [每日一讲] Python系列:字典
#! /usr/bin/python # coding:utf-8 """ DATA STRUCTURE Container: Mapping (Another cont ...
- python学习第九讲,python中的数据类型,字符串的使用与介绍
目录 python学习第九讲,python中的数据类型,字符串的使用与介绍 一丶字符串 1.字符串的定义 2.字符串的常见操作 3.字符串操作 len count index操作 4.判断空白字符,判 ...
- python系列四:Python3字符串
#!/usr/bin/python #Python3 字符串#可以截取字符串的一部分并与其他字段拼接var1 = 'Hello World!'print ("已更新字符串 : ", ...
- Python中字符串使用单引号、双引号标识和三引号标识,什么是三引号?什么情况下用哪种标识?
一.三引号是指三个单引号或者三个双引号: 二.Python中字符串如果以单引号.双引号标识和三引号标识开头,则字符串结尾也必须是对应的标识,不能变更: 三.三者的异同: 1.三者都是字符串,大部分情况 ...
- 【Python】 字符串的相关小知识点
字符串 很久以前,刚接触IT知识的时候,我觉得字符串还有字符变量是很奇葩的存在.数字,数组,字典什么的这些数据类型要不就是自然界固有的要不就是为了计算方便而发明出来的一些数据的组合方式.但是字符串这玩 ...
随机推荐
- 【DSP开发】【VS开发】MUX和DEMUX的含义
MUX和DEMUX Mux 是 Multiplex 的缩写,意为"多路传输",其实就是"混流"."封装"的意思,与"合成" ...
- layer最大话.最小化.还原回调方法
layer.open({ type: 1, title: ‘在线调试‘, content: ‘这里是内容‘, ...
- Mac OS X 11中的/usr/bin 的“Operation not permitted”
更新了 Mac OS X 11后发现,MacVim 不再能够通过Terminal用命令打开了. mvim hello.txt 于是尝试将 mvim 重新复制到/usr/bin/中去 sudo cp - ...
- [Python3] 025 包
目录 1. 模块 1.1 模块是什么? 1.2 为什么用模块? 1.3 如何定义模块? 1.4 如何使用模块? 1.4.1 例子1 1.4.2 例子2 1.4.3 例子3 1.4.4 例子4 1.4. ...
- 移除list里面的值
public class IteratorTest { public static void main(String[] args) { List<String> list = new A ...
- mknod创建设备(加载新的设备驱动时候,通常会用到此命令)
mknod - make block or character special filesmknod [OPTION]... NAME TYPE [MAJOR MINOR] option 有用的就是- ...
- python3.6 使用newspaper库的Article包来快速抓取网页的文章或者新闻等正文
我主要是用了两个方法来抽去正文内容,第一个方法,诸如xpath,css,正则表达式,beautifulsoup来解析新闻页面的时候,总是会遇到这样那样各种奇奇怪怪的问题,让人很头疼.第二个方法是后面标 ...
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 F. Islands
On the mysterious continent of Tamriel, there is a great empire founded by human. To develope the tr ...
- The library 'libhostpolicy.dylib' required to execute the application was not found in
.NET Core应用程序需要runtimeconfig.json文件.此JSON文件配置运行时的选项.没有runtimeconfig.json文件,这将失败. > dotnet Program ...
- Vue.js实现分页
效果图 代码 <!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/1999/ ...