关于 Python 的字符串处理相关的方法还是许多的。因为我正在学习 Python,于是就把 Python 中这些混杂的用于 string 的函数总结出来,在自己忘记的时候便于查找,希望对于有相似需求的人有所帮助。

captalize() 函数

功能

将一个字符串的第一个字母大写

使用方法

str.captalize()

參数

返回值

string

演示样例代码

str = "hello world!"

print "str.capitalize(): ", str.capitalize()

执行结果

tr.capitalize():  Hello world!

center(width, fillchar) 函数

将字符串居中,居中后的长度为 width

功能

将字符串居中。居中后的长度为 width

使用方法

str.center(width[, fillchar])

參数

  • width: 表示字符串总长度
  • fillchar: 使字符串居中所填充的字符。默觉得空格

返回值

返回填充字符后的字符串

演示样例代码

str = "hello world!"

print "str.center(20): ", str.center(20)
print "str.center(20,'-'): ", str.center(20,'-')

执行结果

tr.center(20):      hello world!
str.center(20,'-'): ----hello world!----

count(str, start=0, end=len(string)) 函数

功能

返回该字符串中出现某字符串序列(或字符)的次数

使用方法

str.count(sub, start=0, end=len(string))

參数

  • sub: 被查找的字符串序列
  • start: 開始查找的索引位置,默觉得字符串開始
  • end: 结束查找的索引位置,默觉得字符串结束

返回值

被查找的序列在字符串的查找位置中出现的次数

演示样例代码

str = "hello world! hello world!"

sub = "o"
print "str.count(sub): ", str.count(sub) sub = "hello"
print "str.count(sub, 5) ", str.count(sub, 5)

执行结果

str.count(sub):  4
str.count(sub, 5) 1

decode(encoding=’UTF-8’,errors=’strict’) 函数 & encode(encoding=’UTF-8’,errors=’strict’)

功能

使用特定编码将字符串解码(decode)/编码(encode)

使用方法

str.decode(encoding='UTF-8',errors='strict')

str.encode(encoding='UTF-8',errors='strict')

參数

  • encoding: 使用的编码格式
  • errors: 设置不同的错误处理方法,其它选项有 ignore, replace, xmlcharrefreplace, backslashreplace

返回值

编码/解码后的字符串

演示样例代码

str = "hello world!"

str = str.encode('base64', 'strict')
print "Encoded str: ", str
print "Decoded str: ", str.decode('base64')

执行结果

Encoded str:  aGVsbG8gd29ybGQh

Decoded str:  hello world!

endswith(suffix, start=0, end=len(string)) 函数

功能

推断字符串是否是以某字符串结尾的

使用方法

str.endswith(suffix, start=0, end=len(string))

參数

  • suffix: 被查找的字符串
  • start: 字符串查找的起始位置,默觉得字符串起始位置
  • end: 字符串查找的结束位置。默觉得字符串结束位置

返回值

假设字符串是以 suffix 结尾的返回 True, 否则返回 False

演示样例代码

str = "hello world!"

suffix = "world!"
print str.endswith(suffix) suffix = "llo"
print str.endswith(suffix,0,4)
print str.endswith(suffix,0,5)

执行结果

True
False
True

expandstabs(tabsize=8) 函数

功能

提供自己定义 tab(/t) 长度的方法。默觉得8

使用方法

str.expandtabs(tabsize=8)

參数

  • tabsize: 表示自己定义 tab 的长度

返回值

string

演示样例代码

str = "hello\tworld!"

print "Original str: " + str
print "Defalut expanded tab: " + str.expandtabs();
print "Double expanded tab: " + str.expandtabs(16)

执行结果

Original str: hello world!
Defalut expanded tab: hello world!
Double expanded tab: hello world!

find(str, start=0, end=len(string)) 函数

功能

在字符串的某指定位置查找某字符串

使用方法

str.find(str, start=0, end=len(string))

參数

  • str: 被查找的子字符串
  • start: 查找的起始位置,默觉得字符串起始位置
  • end: 查找的结束位置。默觉得字符串结束位置

返回值

假设查找到。返回该子字符串的索引。未查找到。返回-1

演示样例代码

str = "hello world!"

str1 = "wo"

print str.find(str1)
print str.find(str1, 8)

执行结果

6
-1

index(str, start=0, end=len(string)) 函数

功能

功能上与 find() 同样,仅仅是在未找到子字符串是抛出异常

使用方法

str.index(str, start=0, end=len(string))

參数

同 find()

返回值

假设查找到,返回该子字符串的索引;未查找到,抛出异常

演示样例代码

str = "hello world!"

str1 = "wo"

print str.index(str1)
print str.index(str1, 8)

执行结果

6
Traceback (most recent call last):
File "teststrmethods.py", line 6, in <module>
print str.index(str1, 8)
ValueError: substring not found

isalnum() 函数

功能

推断该字符串是否仅仅是字母数字组合

使用方法

str.isalnum()

參数

返回值

假设该字符串是字母数字组合,返回 True,否则返回 False

演示样例代码

str = "helloworld"
print str.isalnum() str = "hello world"
print str.isalnum() str = "hello123"
print str.isalnum() str = "hello123!"
print str.isalnum()

执行结果

True
False
True
False

isalpha() 函数

功能

推断该字符串是否是字母组合

使用方法

str.isalpha()

參数

返回值

假设该字符串是字母组合,返回 True,否则返回 False

演示样例代码

str = "helloworld"
print str.isalpha() str = "hello world"
print str.isalpha() str = "hello123"
print str.isalpha() str = "hello123!"
print str.isalpha()

执行结果

True
False
False
False

isdigit() 函数

功能

推断该字符串是否仅仅包括数字

使用方法

str.isdigit()

參数

返回值

假设该字符串仅仅包括数字,则返回 True,否则返回 False

演示样例代码

str = "hello123"
print str.isdigit() str = "123456"
print str.isdigit()

执行结果

False
True

islower() 函数

功能

推断该字符串中是否仅仅是小写字母

使用方法

str.islower()

參数

返回值

假设该字符串中仅仅是小写字母,返回True,否则返回False

演示样例代码

str = "hello wolrd!"
print str.islower() str = "Hello Wolrd!"
print str.islower()

执行结果

True
False

isspace() 函数

功能

推断该字符串是否仅仅包括空格

使用方法

str.isspace()

參数

返回值

假设该字符串仅仅包括空格。返回True,否则返回False

演示样例代码

str = "    "
print str.isspace() str = "Hello Wolrd!"
print str.isspace()

执行结果

True
False

istitle() 函数

功能

检查该字符串中的单词是否首字母都大写

使用方法

str.istitle()

參数

返回值

假设该字符串中的单词首字母都大写了,返回True,否则返回False

演示样例代码

str = "Hello world!"
print str.istitle() str = "Hello Wolrd!"
print str.istitle()

执行结果

False
True

isupper() 函数

功能

推断该字符串中的字母是否都是大写

使用方法

str.isupper()

參数

返回值

假设该字符串中的字母都是大写,返回True,否则返回False

演示样例代码

str = "Hello world!"
print str.isupper() str = "HELLO WORLD!"
print str.isupper()

执行结果

False
True

join(seq) 函数

功能

用该字符串连接某字符序列(seq)

使用方法

str.join(sequence)

參数

  • sequence: 被连接的字符序列

返回值

返回连接之后的字符串

演示样例代码

str = "-"
sequence = ("hello", "world", "everyone", "!") print str.join(sequence)

执行结果

hello-world-everyone-!

len(string) 函数

功能

得到该字符串的长度

使用方法

len(str)

參数

返回值

返回该字符串长度

演示样例代码

str = "Hello world!"

print "the length of str: ", len(str)

执行结果

the length of str:  12

ljust(width, fillchar=’ ‘)函数

功能

在字符串的右边填充字符使得字符串达到指定长度

使用方法

str.ljust(width, fillchar=' ')

參数

  • width: 填充后的目标长度
  • fillchar: 用于填充的字符。默觉得空格

返回值

返回填充后的字符串

演示样例代码

str = "Hello world"

print str.ljust(15)
print str.ljust(15,'!')

执行结果

Hello world
Hello world!!!!

本文的版权归作者 罗远航 全部。採用 Attribution-NonCommercial 3.0 License。不论什么人能够进行转载、分享,但不可在未经同意的情况下用于商业用途;转载请注明出处。感谢配合。

Python 字符串 String 内建函数大全(1)的更多相关文章

  1. python字符串(string)方法整理

    python中字符串对象提供了很多方法来操作字符串,功能相当丰富. print(dir(str)) [..........'capitalize', 'casefold', 'center', 'co ...

  2. python - 字符串的内建函数

    # -*- coding:utf-8 -*- '''@project: jiaxy@author: Jimmy@file: study_3_str_内建函数.py@ide: PyCharm Commu ...

  3. python 字符串 string

    字符串 string 语法: a = 'hello world!' b = "hello world!" 常用操作: 1.乘法操作是将字符串重复输出2遍 >>> ...

  4. python字符串常用内建函数总结

    自己总结一些常用字符串函数,理解比较粗糙 1.字符串内建函数-大小写转换函数 (1)str.capitalize Help on method_descriptor: capitalize(...)  ...

  5. python 字符串 string模块导入及用法

    字符串也是一个模块,有自己的方法,可以通过模块导入的方式来调用 1,string模块导入 import string 2,  其用法 string.ascii_lowercase string.dig ...

  6. Python 字符串String相关知识

    test.capitalize( )     |首字母大写 test.lower( )             |全部变成小写(只能处理英文字母) test.casefold( )         | ...

  7. (转)python字符串函数

    原文:https://www.cnblogs.com/emanlee/p/3616755.html https://blog.csdn.net/luoyhang003/article/details/ ...

  8. python字符串截取与替换的例子

    python字符串截取与替换的多种方法 时间:2016-03-12 20:08:14来源:网络 导读:python字符串截取与替换的多种方法,以冒号分隔的字符串的截取方法,python字符串替换方法, ...

  9. Python 字符串处理大全.

    Python 字符串 字符串是Pyhton中常用的数据类型,我们可以使用引号来创建字符串 . 创建字符串很简单 , 就不说了 . Python 访问字符串中的值 鬼叔本着简洁 使用的设计目的 , 在设 ...

随机推荐

  1. B1270 [BeijingWc2008]雷涛的小猫 dp

    这个题的原始方法谁都会,但是n^3会T.之后直接优化,特别简单,就是每次处理出来每层的最大值,而不用枚举.之前没这么做是因为觉得在同一棵树的时候没有下落,所以不能用这个方法.后来想明白了,在同一棵树上 ...

  2. 常用的 Maven 命令

    maven 命令的格式为 mvn [plugin-name]:[goal-name],可以接受的参数如下. -D 指定参数,如 -Dmaven.test.skip=true 跳过单元测试: -P 指定 ...

  3. ThinkPHP __PUBLIC__的定义 __ROOT__等 常用 常量的定义

    '__TMPL__' => APP_TMPL_PATH, // 项目模板目录 '__ROOT__' => __ROOT__, // 当前网站地址 '__APP__' => __APP ...

  4. Dubbo的@Reference和@Service说明---1Reference用在消费者2Service用在提供者【import com.alibaba.dubbo.config.annotation.Service;】

    @Reference 用在消费端,表明使用的是服务端的什么服务@RestControllerpublic class RemoteUserController { @Reference(version ...

  5. 1.java安全框架SHIRO

    1. shiro介绍 Apache Shiro是一个强大且易用的java安全框架,执行身份验证.授权.密码和会话管理. 使用Shiro的易于理解的API,您可以快速.轻松地获得任何应用程序,从最小的移 ...

  6. 改善用户体验 Web前端优化策略总结

    前端是庞大的,包括HTML.CSS.Javascript.Image.Flash等等各种各样的资源.前端优化是复杂的,针对方方面面的资源都有不同的方式.那么,前端优化的目的是什么? 1. 从用户角度而 ...

  7. Laravel5.1学习笔记3 HTTP中间件

    HTTP 中间件 简介 建立中间件 注册中间件 可终止中间件 简介 HTTP 中间件提供一个方便的机制来过滤进入应用程序的 HTTP 请求,例如,Laravel 默认包含了一个中间件来检验用户身份验证 ...

  8. 《CSS Mastery》读书笔记(4)

    第七章 布局   CSS得到一个不好的名声,比较难懂, 一方面由于浏览器的不兼容,另一方面由于网上大量的技巧,每个CSS作者都可以有自己的方法去创建多列布局, 新的CSS开发者只是使用一种方法而不去理 ...

  9. 酷派改变者S1(C105/C105-6/C105-8) 解锁BootLoader 并刷入recovery root

    首先下载好工具链接:https://pan.baidu.com/s/1qZjOCUw 密码:u2dr 备用下载链接:https://pan.baidu.com/s/1pMlmAef 本篇教程教你如何傻 ...

  10. Visual Studio UI Automation 学习(一)

    这几天需要研究自动化测试工具,因为团队开发使用visual studio,所以需要研究一下Visual studio自带的框架. 刚开始安装的时候,没有选自定义安装,所以安装完成后没有找到UI Aut ...