美文网首页
python 字符串

python 字符串

作者: 蜗牛会跑步 | 来源:发表于2020-02-09 22:37 被阅读0次

字符串

a、字符串的创建

  • 可以使用 “ ” 或者' '来创建字符串
#  使用“ ”或者' '创建字符串
str = "hello python"

b、字符串的访问

  • 字符串的索引从0开始,最后一个字符的索引为-1
# 创建一个字符串
str = "hello python"
# 使用索引获取字符串
print(str[1])
# 使用-1获取倒数最后一个字符串
print(str[-1])
print(str[-2])
e
n
o

c、字符串的截取

[ : ] 截取字符串中的一部分,遵循左闭右开原则,str[0,2] 是不包含第 3 个字符的。

# 创建一个字符串
str = "hello python"
# [ : ] 截取字符串中的一部分,**遵循左闭右开原则**
print(str[2:4])
ll

d、字符串的拼接

使用 + 完成字符串的拼接工作。

# 创建字符串1
str1 = "hello"
# 创建字符串2
str2 = "python"
# 使用+进行字符串拼接
str = str1 + " " + str2
print(str)
hello python

e、获取字符串长度

# 获取字符串的长度
str = "hello python"
print(len(str))
12

f、isspace() 方法

使用 isspace()方法判断空字符串,只包含空格或者\t、\n、\r的都认为空字符串。

# 创建一个包含空格的字符串
str_space1 = " "
# 调用isspace()方法来判断字符串是否全是空格
print(str_space1.isspace())
# 包含\r\t\n都认为是空字符串
str_space2 = "\r\n\t"
print(str_space2.isspace())
str_space2 = "\r\n\t  abc"
print(str_space2.isspace())
True
True
False

g、isdecimal() 方法

使用isdecimal()来判断字符串中是否只包含数字。

# 使用isDecimal()来判断字符串中是否只包含数字
str_decimal1="123"
print(str_decimal1.isdecimal())
str_decimal2="123abc"
print(str_decimal2.isdecimal())
True
False

h、startswith()方法 & endswith()

  • str.startswith(substr, beg=0,end=len(string));
    str -- 检测的字符串。
    substr -- 指定的子字符串。
    strbeg -- 可选参数用于设置字符串检测的起始位置。
    strend -- 可选参数用于设置字符串检测的结束位置。
str = "this is string example....wow!!!"
print (str.startswith( 'this' ))   # 字符串是否以 this 开头
print (str.startswith( 'string', 8 ))  # 从第八个字符开始的字符串是否以 string 开头
print (str.startswith( 'this', 2, 4 )) # 从第2个字符开始到第四个字符结束的字符串是否以 this 开头
True
True
False
  • str.endswith(suffix[, start[, end]])
    str -- 待检测的字符串
    suffix -- 该参数可以是一个字符串或者是一个元素。
    start -- 字符串中的开始位置。
    end -- 字符中结束位置。
Str='Runoob example....wow!!!'
suffix='!!'
print (Str.endswith(suffix))
print (Str.endswith(suffix,20))
suffix='run'
print (Str.endswith(suffix))
print (Str.endswith(suffix, 0, 19))
True
True
False
False

i、islower() & isupper() & lower() & upper()

  • islower()方法用来判断一个字符串是否都是小写的字符
# 创建一个只包含小写的字符串
str_lower1 = "hello python"
print(str_lower1.islower())
# 创建一个包含大写字母的字符串
str_lower2 = "Hello python"
print(str_lower2.islower())
True
False
  • isupper()用来判断一个字符串是否都是大写的字符
# 创建一个只包含小写的字符串
str_lower1 = "HELLO PYTHON"
print(str_lower1.isupper())
# 创建一个包含大写字母的字符串
str_lower2 = "Hello python"
print(str_lower2.isupper())
True
False
  • lower()用来转换所有大写字符为小写字符
# 创建一个包含大小写的字符串
str = "Hello python"
# 将所有的大写字符转换成小写字符
print(str.lower())
hello python
  • upper()用来转换所有的小写字符为大写字符
# 创建一个包含大小写的字符串
str = "Hello python"
# 将所有的小写字符转换成大写字符
print(str.upper())
HELLO PYTHON

j、find() & rfind() & index() & rindex() & replace()

  • str.find(str, beg=0, end=len(string))
    str -- 指定检索的字符串
    beg -- 开始索引,默认为0。
    end -- 结束索引,默认为字符串的长度。
str1 = "Runoob example....wow!!!"
str2 = "exam";
 
print (str1.find(str2))
print (str1.find(str2, 5))
print (str1.find(str2, 10))
7
7
-1
  • str.rfind(str, beg=0 end=len(string))
    str -- 查找的字符串
    beg -- 开始查找的位置,默认为0
    end -- 结束查找位置,默认为字符串的长度
str1 = "this is really a string example....wow!!!"
str2 = "is"

print (str1.rfind(str2))

print (str1.rfind(str2, 0, 10))
print (str1.rfind(str2, 10, 0))

print (str1.find(str2))
print (str1.find(str2, 0, 10))
print (str1.find(str2, 10, 0))
5
5
-1
2
2
-1

k、strip() & rstrip() & lstrip()

  • str.strip([chars])
    chars -- 移除字符串头尾指定的字符序列。
str = "*****this is **string** example....wow!!!*****"
print (str.strip( '*' ))  # 指定字符串 *
this is **string** example....wow!!!

l、split() & joint()

  • str.split(str="", num=string.count(str))
    str -- 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
    num -- 分割次数。默认为 -1, 即分隔所有。
str = "this is string example....wow!!!"
print (str.split( ))       # 以空格为分隔符
print (str.split('i',1))   # 以 i 为分隔符
print (str.split('w'))     # 以 w 为分隔符
['this', 'is', 'string', 'example....wow!!!']
['th', 's is string example....wow!!!']
['this is string example....', 'o', '!!!']
  • str.join(sequence)
    sequence -- 要连接的元素序列。
s1 = "-"
s2 = ""
seq = ("r", "u", "n", "o", "o", "b") # 字符串序列
print (s1.join( seq ))
print (s2.join( seq ))
r-u-n-o-o-b
runoob

部分案例来自于菜鸟教程

相关文章

  • python基础知识(3)

    python字符串 python转义字符 python字符串运算符 python字符串格式化 python格式化操...

  • python count()方法详解

    Python count()方法 Python 字符串 描述 Python count() 方法用于统计字符串里某...

  • python字符串格式化符号与内建函数资料表

    python字符串格式化符号: Python 的字符串内建函数 Python 的字符串常用内建函数如下:

  • 字符串操作方法

    Python3字符串 Python访问字符串中的值 Python中的字符串用单引号(')或双引号(")括起来,同时...

  • python字符串相关函数

    Python3字符串 Python访问字符串中的值 Python中的字符串用单引号(')或双引号(")括起来,同时...

  • 2018-09-28自学习资料

    Python3字符串 Python访问字符串中的值 Python中的字符串用单引号(')或双引号(")括起来,同时...

  • 字符串内置函数

    Python3字符串 Python访问字符串中的值 Python中的字符串用单引号(')或双引号(")括起来,同时...

  • 2018-07-18 字符串资料(函数方法)

    Python3字符串资料 Python访问字符串中的值 Python中的字符串用单引号(')或双引号(")括起来,...

  • day4 字符串自主操作

    Python3字符串 Python访问字符串中的值 Python中的字符串用单引号(')或双引号(")括起来,同时...

  • Day3-整理

    Python3字符串 Python访问字符串中的值 Python中的字符串用单引号(')或双引号(")括起来,同时...

网友评论

      本文标题:python 字符串

      本文链接:https://www.haomeiwen.com/subject/oaonxhtx.html