前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python基础(3)——字符串

Python基础(3)——字符串

作者头像
羊羽shine
发布2019-05-28 13:30:38
3620
发布2019-05-28 13:30:38
举报
文章被收录于专栏:Golang开发Golang开发
字符串语法

双引号或者单引号中的数据,就是字符串

代码语言:javascript
复制
str = "hellow world"
string = 'hellow python'
字符串的输入输出
代码语言:javascript
复制
name = input("请输入姓名:")
print(name)
age = input("请输入年龄:")
print(age)
字符串格式化

字符串格式化使用操作符百分号%实现

代码语言:javascript
复制
print("hello,%s" % "world")

浮点型数字指定精度

代码语言:javascript
复制
print("圆周率PI的值是%.2f" % 2.1415926)

字符串和元组

代码语言:javascript
复制
print("我的名字叫%s,我今年%d,我现在在%s" % ("bx", 25, "beijing"))
字符串索引
代码语言:javascript
复制
str = "123456"   
print(str[2])    
print(str[-1])   
字符串切片

切片的语法:[起始:结束:步长],选取的区间属于左闭右开型,即从"起始"位开始,到"结束"位的前一位结束(不包含结束位本身)。

代码语言:javascript
复制
name = "hello world"
print(name[0:3])
多行数据字符串表示

'''...'''的格式表示多行内容

代码语言:javascript
复制
str = """1
22
333
4444
"""
print(str)
字符串编码和解码
代码语言:javascript
复制
s =u'你好'
e = s.encode('utf-8')
print(e)
d = e.decode('utf-8')
print(d)
#b'\xe4\xbd\xa0\xe5\xa5\xbd'
#你好

字符串常用方法

find

检测 str 是否包含在 mystr中,如果是返回开始的索引值,否则返回-1,index跟find()方法一样,只不过如果str不在 mystr中会报一个异常.

代码语言:javascript
复制
str = "123456"          
print(str.find("2"))    
print(str.index("3"))   
count

返回 str在start和end之间 在 mystr里面出现的次数

代码语言:javascript
复制
str = "1234565"         
print(str.count("5"))   
startwith

字符串是否是以 prefix 开头, 是则返回 True,否则返回 False

代码语言:javascript
复制
str = "hello,python"            
print(str.startswith("hello"))  
True
endswith

字符串是否是以 suffix结束,如果是返回True,否则返回 False.

代码语言:javascript
复制
str = "hello,python"          
print(str.endswith("world"))  
join
代码语言:javascript
复制
iterStr = "123"            
str = ","                  
print(str.join(iterStr)) 
1,2,3  
isalpha()

字符串是否全部是字母

代码语言:javascript
复制
print(str.isalpha())//False
str = "HelloPython"
print(str.isalpha())//True
isdigit()

字符串是否全部是数字

代码语言:javascript
复制
str = "0123456789"
print(str.isalnum())
capitalize

首字母变成大写,其余字母小写

代码语言:javascript
复制
str = "hello,Python"     
print(str.capitalize())  
Hello,python
title

非字母隔开的部分,首字母大写,其余字母小写

代码语言:javascript
复制
str = "hello,python"  
print(str.title())    
Hello,Python
lower

将字符串都转成小写

代码语言:javascript
复制
str = "Hello,Python"    
print(str.lower())      
hello,python
upper

将字符串中所有的小写转成大写

代码语言:javascript
复制
str = "hello,python
print(str.upper()) 
HELLO,PYTHON
swapcase

将大写转成小写,小写转成大写

代码语言:javascript
复制
str = "hELLO,pYTHON"    
print(str.swapcase())   
Hello,Python
replace

把 string 中的 str1 替换成 str2,如果 count 指定,则替换不超过 count 次.

代码语言:javascript
复制
str = "hello world"                     
print(str.replace("world", "python"))   
split

以 sep 为分隔符切片 str,如果 maxsplit有指定值,则仅分隔 maxsplit 个子字符串

代码语言:javascript
复制
str = "hello,python"  
print(str.split(",")) 
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.07.11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 字符串语法
  • 字符串的输入输出
  • 字符串格式化
  • 字符串索引
  • 字符串切片
  • 多行数据字符串表示
  • 字符串编码和解码
  • 字符串常用方法
    • find
      • count
        • startwith
          • endswith
            • join
              • isalpha()
                • isdigit()
                  • capitalize
                    • title
                      • lower
                        • upper
                          • swapcase
                            • replace
                              • split
                              领券
                              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档