python-字符串方法

python-字符串方法,格式化输出
  学了这么久了,想总结一下python基础性知识,写的不好或者不对的地方,大家也可以留言让其补充、修改:

  1. 字符串的定义:(ctrl+/)注释/取消注释

             单引号、双引号、三引号(其中三引号也可以用来做注释)
    

str = “aad222”

str = ‘ss22’

str = ‘’’ ss222’’’

  1. 字符串拼接(+号)

    print(‘hello’+‘world’) ---- helloworld

    print(‘hello’+‘3’) ---- hello3

    print(‘hello’*3) ----- hellohellohello

    print('hello '*3) ---- hello hello hello

    print(3,‘hello’) — 3 hello

    几种报错的拼接字符串:

         print('hello'+3) ---  报错。类型不一致。和前面的保持一致(typeerror:must be str,not int)
    
         print(3+'hello') ---  报错,类型不一致。和前面保持一致(typeerror:unsupported operand type(s) for +:'int' and 'str')
    

3.序列操作

    序列定义:一个序列,若干个元素组成;一般下标从0开始

   常用操作:

    a.获取元素

str[10]/str[len(str)-1]/str[-1]

    b.求元素个数

len(str)

c. 获取某元素的下标

str.index(‘b’)

   d.切片操作

      string[start:end]:从start开始,到end结束,但不包含end

      string[start:]:从start开始,到字符串结束

string[:end]:从第一个字符开始,到end位置结束,但不包括end

string[start🔚步长]:步长默认是1

字符串倒序:string[::-1]

正下表:从左向右:0,1,2,3,4

      负下标:从右向左:-1,-2,-3,-4

in : 成员运算符 - 如果字符串中包含给定的字符返回 True

可以使用find方法,不等于-1则表示找到

例如: str = ‘hello’

'h’ in str ----- True

    not in : 成员运算符 - 如果字符串中不包含给定的字符返回 True

例如: str = ‘hello’

‘a’ not in str – True

   r/R : 原始字符串 - 原始字符串:所有的字符串都是直接按照字面的意思来使用,没有转义特殊或不能打印的字符。 原始字符串除在字符串的第一个引号前                加上字母"r"(可以大小写)以外,与普通字符串有着几乎完全相同的语法。

                例如: open(r"d:\project\aa.txt")

    % :  格式化字符串

4.Python转义字符

(在行尾时) 续行符
\ 反斜杠符号
’ 单引号
" 双引号
\a 响铃
\b 退格(Backspace)
\e 转义
\000 空
\n 换行
\v 纵向制表符
\t 横向制表符
\r 回车
\f 换页
\oyy 八进制数,yy代表的字符,例如:\o12代表换行
\xyy 十六进制数,yy代表的字符,例如:\x0a代表换行
\other 其它的字符以普通格式输出
5.字符串常用的方法

   1> str.count():计算字符串中包含多少个指定的子字符串

info = “abcdefa”

print (info.count(‘a’)) ---- 2

2> str.startswith():检查字符串是否以指定的字符串开头

info = “this is tom”

print (info.startswith(“this”)) — True

3> str.endswith() :检查字符串是否以指定的字符串结尾

info = “this is tom”

print (info.endswith(“tom”)) – True

4> str.find() :返回指定的子字符串在字符串中出现的位置

“123456789”.find(“456”) ---- 3

“ok,good,name”.find(’,’) ---- 2 (有多个则返回第一个)

“ok,good,name”.find(‘o’,3) – 4(指定位置开始找,正序找)

“ok,good,name”.find(‘o’,-1) – 0(指定位置开始查找:倒序只找最后一个元素)

5> str.isalpha() :检查字符串中是否都是字母

“abc1”.isaplha() – False

6> str.isdigit() :检查字符串中是否都是数字

“123123”,isdigit() — True

7> str.join():将sequence序列类型的参数的元素字符串合并(连接到一个字符串)

print("##".join([‘i’,‘like’,‘play’,‘football!’])) – i##like##play##football

print("##".join((‘i’,‘like’,‘play’,‘football!’))) – i##like##play##football

8> str.split():将字符串分割为几个子字符串,参数为分隔符,返回list列表

被分割的切点会被拿掉

info = 'name is tom ’

print(info.split(’ ‘)) ---- [‘name’,‘is’,‘tom’,’’]

9> str.lower() :将字符串里面如果有大写字母的全部转为小写字母

10> str.opper():将字符串里面如果有小写字母的全部转为大写字母

11> str.replace():替换字符串里面指定的子字符串

info = “name is tom”

print(info.replace(‘tom’,‘jack’)) — name is jack

12> str.strip() :将字符串前置空格和后置空格删除

" good ".strip() — ‘good’

13> str.lstrip():去除前置空格

" good ".strip() — 'good ’

14> str.rstrip():去除后置空格

" good ".strip() — ’ good’

6.字符串的格式化

 1> %s,%d,%f,%x

a > %s:字符串

name =‘tom’

age = 18

info = “名字是:%s,年龄是:%d”%(name,age)

print (info)

结果:名字是:Tom,年龄是:18

b> %d:十进制

‘%d’%56 --输出的是字符串‘56’

‘%10d’56 – 最小宽度,不足空格补齐 ----- 结果:‘ 56’

‘%010d’%56 – 补零 ------ 结果:‘0000000056’

c> %f:浮点数

默认小数点后面6位

“%f”%3.1415926 – 3.141593

“%.3f”%3.141592 – 3.142

指定小数点后面位数

“%9.2f”%1234.567890 – ’ 1234.57’

“%09.2f”%1234.567890 – ‘001234.57’

d> %x:无符号的十六进制,(x/X代表了转换后的十六进制字符的大小写)

“%x”%108 – ‘6c’

“%X”%108 --‘6C’

“%#X”%108 – ‘0X6C’

“%#x”%108 – ‘0x6c’

   2> .format()

           格式: format % values

format:是等待格式化的字符串,由包含%号的占位符的字符串组成的

values:可以是普通的数值对象、字符串对象

tuple,表示多个对象填充format里面的占位符%

         a> 顺序填充:.format(name,age)值可以多但是不能少,输出格式左、右、中间对齐::< ,:>,^

                  

          b>  下标填值

                   

          c>  变量填值

                    

         d> 特殊用法,python3.6以后才可以使用,前面加f
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值