《Modern Python Cookbook》(Python经典实例)笔记1.6 改写字符串

本文介绍了Python中字符串的基本操作,包括使用partition方法分割字符串、replace方法替换字符串中的内容、upper及lower方法转换字符大小写、strip方法去除字符串两端指定字符以及isnumeric方法判断字符串是否全为数字。

字符串是不可变类型,不能原位(inplace)修改字符串,即不能直接以索引或切片形式元素赋值直接替换字符串的内容。所以,修改字符串的操作都将新产生一个新的字符串对象。

In [1]: s='abc'
In [2]: s[0]='b'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-7f020176d33f> in <module>
----> 1 s[0]='b'
TypeError: 'str' object does not support item assignment

一、字符串.partition方法根据指定分隔符将字符串分割为三元组

partition() 方法用来根据指定的分隔符将字符串进行分割。

如果字符串包含指定的分隔符,则返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。

格式:str.partition(sep)
In [7]: title = '1.3'
In [8]: title.partition('.')
Out[8]: ('1', '.', '3')
In [9]: title
Out[9]: '1.3'

二、字符串.replace方法将把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次

格式:str.replace(old, new[, max])

In [9]: title
Out[9]: '1.3'
In [10]: title.replace('.',':')
Out[10]: '1:3'
In [11]: title
Out[11]: '1.3'

三、字符串.upper方法、lower方法将字符串字符全部改写为大写或者小写

格式:str.upper() 或str.lower()

In [13]: title='Abc'
In [14]: title.upper()
Out[14]: 'ABC'
In [15]: title.lower()
Out[15]: 'abc'
In [16]: title
Out[16]: 'Abc'

四、字符串.strip方法移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。

格式:str.strip(substr)

In [17]: title="-a-b-c-"
In [18]: title.strip('-')
Out[18]: 'a-b-c'
In [19]: title
Out[19]: '-a-b-c-'
In [23]: title=" a-b-c     "
In [24]: title.strip()
Out[24]: 'a-b-c'

五、字符串.isnumeric方法检测字符串所有元素是否都为数字

In [25]: title='1a1'
In [26]: title.isnumeric()
Out[26]: False
In [28]: title='123'
In [29]: title.isnumeric()
Out[29]: True

字符串的切片标记法中,所有字符的索引 i 都在 start≤i<end 范围内,即切片范围是左闭右开或者说右半开空间。闭区间是指端点包含在内,开区间是指端点不包括在内。

Key Features Develop succinct, expressive programs in Python Learn the best practices and common idioms through carefully explained and structured recipes Discover new ways to apply Python for the new age of development Book Description Python is the preferred choice of developers, engineers, data scientists, and hobbyists everywhere. It is a great scripting language that can power your applications and provide great speed, safety, and scalability. By exposing Python as a series of simple recipes, you can gain insight into specific language features in a particular context. Having a tangible context helps make the language or standard library feature easier to understand. This book comes with over 100 recipes on the latest version of Python. The recipes will benefit everyone ranging from beginner to an expert. The book is broken down into 13 chapters that build from simple language concepts to more complex applications of the language. The recipes will touch upon all the necessary Python concepts related to data structures, OOP, functional programming, as well as statistical programming. You will get acquainted with the nuances of Python syntax and how to effectively use the advantages that it offers. You will end the book equipped with the knowledge of testing, web services, and configuration and application integration tips and tricks. The recipes take a problem-solution approach to resolve issues commonly faced by Python programmers across the globe. You will be armed with the knowledge of creating applications with flexible logging, powerful configuration, and command-line options, automated unit tests, and good documentation. What you will learn See the intricate details of the Python syntax and how to use it to your advantage Improve your code readability through functions in Python Manipulate data effectively using built-in data structures Get acquainted with advanced programming techniques in Python Equip yourself with functional and statistical programming features Write proper tests to be sure a program works as advertised Integrate application software using Python Table of Contents Chapter 1. Numbers, Strings, and Tuples Chapter 2. Statements and Syntax Chapter 3. Function Definitions Chapter 4. Built-in Data Structures – list, set, dict Chapter 5. User Inputs and Outputs Chapter 6. Basics of Classes and Objects Chapter 7. More Advanced Class Design Chapter 8. Functional and Reactive Programming Features Chapter 9. Input/Output, Physical Format, and Logical Layout Chapter 10. Statistical Programming and Linear Regression Chapter 11. Testing Chapter 12. Web Services Chapter 13. Application Integration
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值