- 浏览: 318765 次
文章分类
最新评论
-
ZT71363387:
多谢,帮我解决了抄送人收不到邮件的问题
python smtplib -
dsjt:
pythonw.exe 运行后无反应。版本不匹配啊。
python+PyQT+Eric安装配置 -
dsjt:
xp 32位系统安装eric后,目录下没有 .bat文件是怎么 ...
python+PyQT+Eric安装配置
收集常用的Python 内置的各种字符串处理 函数的使用方法
str='python String function'
- 生成字符串变量str='python String function'
- 字符串长度获取:len(str)
例:print '%s length=%d' % (str,len(str))
- 字母处理
全部大写:str.upper()
全部小写:str.lower()
大小写互换:str.swapcase()
首字母大写,其余小写:str.capitalize()
首字母大写:str.title()
print '%s lower=%s' % (str,str.lower())
print '%s upper=%s' % (str,str.upper())
print '%s swapcase=%s' % (str,str.swapcase())
print '%s capitalize=%s' % (str,str.capitalize())
print '%s title=%s' % (str,str.title()) -
格式化相关
获取固定长度,右对齐,左边不够用空格补齐:str.ljust(width)
获取固定长度,左对齐,右边不够用空格补齐:str.ljust(width)
获取固定长度,中间对齐,两边不够用空格补齐:str.ljust(width)
获取固定长度,右对齐,左边不足用0补齐
print '%s ljust=%s' % (str,str.ljust(20))
print '%s rjust=%s' % (str,str.rjust(20))
print '%s center=%s' % (str,str.center(20))
print '%s zfill=%s' % (str,str.zfill(20)) -
字符串搜索相关
搜索指定字符串,没有返回-1:str.find('t')
指定起始位置搜索:str.find('t',start)
指定起始及结束位置搜索:str.find('t',start,end)
从右边开始查找:str.rfind('t')
搜索到多少个指定字符串:str.count('t')
上面所有方法都可用index代替,不同的是使用index查找不到会抛异常,而find返回-1
print '%s find nono=%d' % (str,str.find('nono'))
print '%s find t=%d' % (str,str.find('t'))
print '%s find t from %d=%d' % (str,1,str.find('t',1))
print '%s find t from %d to %d=%d' % (str,1,2,str.find('t',1,2))
#print '%s index nono ' % (str,str.index('nono',1,2))
print '%s rfind t=%d' % (str,str.rfind('t'))
print '%s count t=%d' % (str,str.count('t')) -
字符串替换相关
替换old为new:str.replace('old','new')
替换指定次数的old为new:str.replace('old','new',maxReplaceTimes)
print '%s replace t to *=%s' % (str,str.replace('t', '*'))
print '%s replace t to *=%s' % (str,str.replace('t', '*',1)) -
字符串去空格及去指定字符
去两边空格:str.strip()
去左空格:str.lstrip()
去右空格:str.rstrip()
去两边字符串:str.strip('d'),相应的也有lstrip,rstrip
str=' python String function '
print '%s strip=%s' % (str,str.strip())
str='python String function'
print '%s strip=%s' % (str,str.strip('d')) -
按指定字符分割字符串为数组:str.split(' ')
默认按空格分隔
str='a b c de'
print '%s strip=%s' % (str,str.split())
str='a-b-c-de'
print '%s strip=%s' % (str,str.split('-')) -
字符串判断相关
是否以start开头:str.startswith('start')
是否以end结尾:str.endswith('end')
是否全为字母或数字:str.isalnum()
是否全字母:str.isalpha()
是否全数字:str.isdigit()
是否全小写:str.islower()
是否全大写:str.isupper()
str='python String function'
print '%s startwith t=%s' % (str,str.startswith('t'))
print '%s endwith d=%s' % (str,str.endswith('d'))
print '%s isalnum=%s' % (str,str.isalnum())
str='pythonStringfunction'
print '%s isalnum=%s' % (str,str.isalnum())
print '%s isalpha=%s' % (str,str.isalpha())
print '%s isupper=%s' % (str,str.isupper())
print '%s islower=%s' % (str,str.islower())
print '%s isdigit=%s' % (str,str.isdigit())
str='3423'
print '%s isdigit=%s' % (str,str.isdigit())
还有其他常见的Python字符串处理 函数的话不定期更新。
发表评论
-
lxml.etree
2017-06-21 10:48 631http://www.cnblogs.com/bluesc ... -
Python dictdiffer模块比较两个字典
2017-03-04 17:51 3558http://dictdiffer.readthedocs. ... -
python 判断一个对象是否可迭代
2017-01-23 09:44 657如何判断一个对象是可迭代对象呢?方法是通过collectio ... -
给python交互式命令行增加自动补全和命令历史
2017-01-22 10:09 688在用户目录下新建".pythonstartup& ... -
python 字典格式化
2017-01-16 13:57 420import pprint data = {'a': ... -
python最简洁的条件判断语句写法
2017-01-11 20:11 1174这篇文章主要介绍了Python返回真假值(True or ... -
python 格式化json
2016-12-19 20:07 570#!/usr/bin/env python # -*- ... -
python下载文件的三种方法
2016-11-18 10:21 411Python开发中时长遇到要下载文件的情况,最常用的方法就是 ... -
python 格式化字典字符串
2016-11-13 15:27 351version = {'a': 1, 'b': 2, 'c' ... -
python __fatal退出函数
2016-11-13 14:41 644def __fatal(exitcode, message) ... -
Python logging
2016-09-18 18:13 499#!/usr/bin/env python # -*- ... -
思考:如果list中既包含字符串,又包含整数,由于非字符串类型没有lower()方法,所以列表生成式会报错:
2016-06-24 18:30 2052>>> L = ['Hello', 'Wor ... -
去除重复字符串并保持原来顺序输出
2016-06-23 17:53 1209#!/usr/bin/env python # -*- c ... -
Python getpass 输入密码
2016-03-28 20:35 631Python有个自带的getpass模块: impor ... -
优秀Python学习资源收集汇总(强烈推荐)
2016-02-29 15:15 708Python是一种面向对象、直译式计算机程序设计语言。它的 ... -
python 模拟登录iteye博客
2016-01-07 11:32 642#!/usr/bin/python # -*- codi ... -
Python之路【目录】
2016-01-02 00:41 642http://www.cnblogs.com/wupeiq ... -
Python 正则 提取由start开始到end结束的行
2015-12-16 13:51 721import re s=open(r"bug.t ... -
Python文件替代fileinput模块
2015-12-12 12:05 1397Python文件替代fileinput模块 一,文件替代: ... -
linecache,想读某行周围的哪行都可以
2015-12-12 12:04 716import linecache x=linecache ...
相关推荐
Python内置的字符串处理函Python是一种解释型、面向对象、动态数据类型的高级程序设计语言。自从20世纪90年代初Python语言诞生至今,它逐渐被广泛应用于处理系统管理任务和Web编程。
Python字符串处理是编程中常见的任务,它涉及到对文本数据的操作,包括查找、连接、替换和分割等。在Python中,有多种内置函数可以帮助我们高效地完成这些任务。下面我们将详细探讨`find`、`join`、`replace`以及`...
Python字符串操作常用函数,包含了检索、统计、分割、替换、大小写转换、对齐,空格删除、字符串判断(头尾+组成)
以上就是Python字符串处理函数的简明总结。Python通过这些内置的方法提供了强大的文本处理功能,极大地简化了对字符串的操作。熟练掌握并灵活运用这些字符串处理方法,对于提高编程效率和处理文本数据具有重要意义。...
C语言字符串转换为Python字符串的方法 C语言字符串转换为Python字符串是指将C语言中的字符串数据转换为Python中的字符串对象,以便在Python环境中使用。下面详细介绍了C语言字符串转换为Python字符串的方法。 使用...
三、replace:返回某字符串的所有匹配项均被替换之后得到的字符串。 复制代码 代码如下:‘This is a test’.replace(‘is’,’are’) 四、split:将字符串分割成序列 复制代码 代码如下:‘1+2+3+4+5’.split(‘+’) ...
python字符串查找函数的使用 打开Python开发工具IDLE,新建‘findstr.py’文件,并写代码如下: s ='/ab/bx,.s' print (s.find('/x')) 注意find是匹配子字符串,而不是匹配第一个字符 F5运行程序,打印出-1,代表...
Python 中可以使用 LevenshteinDistance 函数来计算两个字符串之间的编辑距离。 FuzzyWuzzy 是一个 Python 库,用于字符串模糊匹配。它提供了多种字符串相似度计算方法,包括 ratio、partial_ratio、token_sort_...
Python 字符串处理实例 Python 中的字符串处理是 Python 编程语言中非常重要的一部分。字符串是 Python 中最基本的数据类型之一,它可以表示文本、数字、符号等。 Python 提供了多种字符串处理方法,本文将对 ...
Python 字符串处理及相关函数 Python 中的字符串处理是编程中非常重要的一部分,Python 提供了多种字符串处理函数,包括 capitalize()、upper()、lower()、title() 等,以下是对这些函数的详细介绍: 1. ...
"Python 字符串、内建函数" Python 字符串是 Python 中最常用的数据类型。我们可以使用引号('或")来创建字符串。创建字符串很简单,只要为变量分配一个值即可。例如:var1 = 'Hello World!'var2 = "Python Runoob" ...
Python字符串常用函数 Python字符串是Python编程语言中最基本也是最重要的一种数据类型。字符串常用函数是指那些可以对字符串进行操作和处理的函数。在Python中,字符串常用函数包括replace、capitalize、split、...
标题“字符串处理函数(9KB)”暗示我们将探讨一系列用于操作和管理字符串的函数,这些函数可能包含在某个编程语言的库或者特定的工具包中。描述中的“9KB”可能是指相关代码或资源的大小,这通常表示这是一个相对较小...
PYTHON常用字符串处理函数 在历史上string类在python中经历了一段轮回的历史。在最开始的时候,python有一个专门的string的module,要使用string的方法要先import,但后来由于众多的python使用者的建议,从python2.0...
在给定的文件内容中,我们可以看到有关Python字符串操作的知识点。这些操作涉及到C语言风格的字符串函数模拟,但它们实际上并不是Python原生的字符串操作方法。Python语言本身就拥有强大的字符串处理能力,不过,...
python join函数 在Python中,join()函数用于将字符串列表或可迭代对象中的元素连接起来形成一个新的字符串。
Python
3. **内置字符串函数**: - `chr()` 函数接收一个整数(通常是ASCII或Unicode编码),并返回对应的字符。例如: ```python print(chr(97)) # 输出 'a' print(chr(8364)) # 输出 '€' ``` - `ord()` 函数接收一...
Python字符串是编程中常用的数据类型,它用于存储和处理文本信息。在Python中,字符串提供了丰富的内置函数,方便我们进行各种操作。以下是一些重要的字符串函数及其用途: 1. `replace(string, old, new[, ...