特别特别篇1(字符串)

特别特别篇(字符串)

感觉天天都在处理这个家伙,只能再学字符串。谁让字符串是用的最频繁的,所以是重点中的重点

1字符串的创建(简单)

var1 = 'Hello World!'

var2 = "Python Programming"

var3="""New Settle"""

可以看成是c语言的字符数组

 

2字符串的特殊操作(摘自python2.7英文手册)

a = 'Hello '

b = 'Python'

操作符

功能描述

例子

+

连接字符串

a + b   HelloPython

*

复制字符串连接到一起

a*2    HelloHello

[]

切字母

a[1]    e

[ : ]

范围切片

a[1:4]  ell

in

判断字符是否在字符串中

     返回为true

not in

判断字符是否不在字符串中

不在   返回为true

r/R

脱意符

print r'/n' prints /n and print R'/n' prints /n

%

格式化输出

后面单独拿出来讨论

 

3字符串相关的方法(真多,有些要是用c语言封装一下,感觉应该是很好的库函数)

测试字符串:

str = "Shuai and NB"

3.1 str.capitalize()

将字符串的第一个字母变成大写字母,其他的都变成小写字母

3.2 str.center(width[, fillchar])

在指定宽度内,将字符串2边填满fillchar

3.3 str.count(sub[, start[, end]])

统计sub字符串出现的次数, startend是切片用的,表示在什么范围内统计

3.4str.decode([encoding[, errors]]) str.encode([encoding[, errors]])

转换字符串的编码和译码格式,这个函数功能很强大滴,encoding可以是很多的方式的转换,除了咱们原有的码值转换外,还有可以做其他格式的转换:

实例:

#!/usr/bin/python

str = "this is string example"

str = str.encode('base64','strict')

 

print "Encoded String: " + str

print "Decoded String: " + str.decode('base64','strict')

输出:

Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=

Decoded String: this is string example

 

摘自官方的encoding列表:

Codec

Aliases

Operand type

Purpose

base64_codec

base64, base-64

byte string

Convert operand to MIME base64

bz2_codec

bz2

byte string

Compress the operand using bz2

hex_codec

hex

byte string

Convert operand to hexadecimal representation, with two digits per byte

idna

 

Unicode string

Implements RFC 3490, see also encodings.idna

mbcs

dbcs

Unicode string

Windows only: Encode operand according to the ANSI codepage (CP_ACP)

palmos

 

Unicode string

Encoding of PalmOS 3.5

punycode

 

Unicode string

Implements RFC 3492

quopri_codec

quopri, quoted-printable, quotedprintable

byte string

Convert operand to MIME quoted printable

raw_unicode_escape

 

Unicode string

Produce a string that is suitable as raw Unicode literal in Python source code

rot_13

rot13

Unicode string

Returns the Caesar-cypher encryption of the operand

string_escape

 

byte string

Produce a string that is suitable as string literal in Python source code

undefined

 

any

Raise an exception for all conversions. Can be used as the system encoding if no automatic coercion between byte and Unicode strings is desired.

unicode_escape

 

Unicode string

Produce a string that is suitable as Unicode literal in Python source code

unicode_internal

 

Unicode string

Return the internal representation of the operand

uu_codec

uu

byte string

Convert the operand using uuencode

zlib_codec

zip, zlib

byte string

Compress the operand using gzip

 

3.5str.endswith(suffix[, start[, end]])

判断字符串的尾缀,如果和给出suffix一样,责返回为真,否则为假

3.6str.expandtabs([tabsize])

用指定的空格数来替换文件里面的/t 就是所谓的tab了,如果不设置的话,tabsize8

3.7str.find(sub[, start[, end]])

找到substring中的位置,找到的话返回所在的位置,如果没找到的话,返回-1

   str.rfind(str, beg=0,end=len(string))

从尾巴向前找到substring中的位置,找到的话返回所在的位置,如果没找到的话,返回-1

3.8str.format(*args, **kwargs)

没弄明白

3.9str.index(sub[, start[, end]])

find方法一样,当没找到的时候返回的是个ValueError异常,不是-1

str.rindex( str, beg=0, end=len(string))

index方法一样,从后向前查找

3.10判断字母的函数

str.isalnum()              判断字符串是不是由字母数字组成

str.isalpha()               判断字符串是不是由字母组成

str.isdigit()                判断字符串是不是由数字组成

str.islower()               判断字符串是不是都是小写

str.isspace()              判断字符串是不是都是空格

str.istitle()                  判断字符串是不是每一个单词第一个字母都大写

str.isdecimal()           判断unicode字符串是不是由10进制数组成

str.isupper()              判断字符串是不是都是大写

3.11str.join(iterable)

iterablestr的每个单词串起来~

实例:

str = "-"

seq = ("a", "b", "c")

输出:

a-b-c

3.12str.ljust(width[, fillchar])

返回从第一个字符到给定的width的字符,如果字符串没有给出的width那么长,字符串在左面,剩下的位置用fillchar右面填写

str.ljust(width[, fillchar])

返回从第一个字符到给定的width的字符,如果字符串没有给出的width那么长,字符串在右面,剩下的位置用fillchar在左面填写

3.13str.lower()

将字符串转换为小写

str.upper()

将字符串转换为大写

2.14str.lstrip([chars])       是把给出的char从字符串的开头给剥离了

str.rstrip([chars])     是把给出的char从字符串的尾巴给剥离了

str.strip([chars])       是把给出的char从字符串的开头和尾巴给剥离了

2.15str.title()

将字符串的每一个单词的第一个字母变成大写

2.16str.replace(old, new[,max])

old字符串替换成new,最多替换几个,比如一个文章里有很多old字符串

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值