美文网首页
1. 字符串

1. 字符串

作者: Miacis | 来源:发表于2019-02-18 20:05 被阅读0次

创建

s1 = 'guvmao'
s2 = "guvmao"
"""

简单操作

\ 转义符

testimony = 'This shirt doesn\'t fit me'
words = 'hello \nworld'

+ 拼接

In [33]: print('hello' + 'world')
Out[33]: helloworld

* 复制

In [35]: print('*' * 20)
In [36]: print('guvmao' * 3)
Out[36]: guvmaoguvmaoguvmao

字符串 和 0 或者 负数相乘,会得到一个空字符串

In [76]: 'hey' * 0
Out[76]: ''

In [77]: 'hey' * -3
Out[77]: ''

进阶操作

Python 中的数据结构 序列类型

# 使用切片获取多个元素
In [79]: i = 'guvmao'
In [80]: print(i[:3])
Out[80]: guv

下面这样的操作,是找不到我的

s1[-1:-3]
# 获取字符串的长度,包含空格和换行符
len(s1)
利用字符串对象的方法

split

In [93]: url = 'www.qfedu.com 千锋官网'
In [94]: url2 = url.split('.')
In [95]: print(url2)
Out[95]: ['www', 'qfedu', 'com 千锋官网']

In [96]: url3 = url.split('.', 1)  # 只对第一个出现的"."进行分割
In [97]: print(url3)
Out[97]: ['www', 'qfedu.com 千锋官网']

In [98]: url4 = url.rsplit('.', 1)  # rsplit 从右向左分割
In [99]: print(url4)
Out[99]: ['www.qfedu', 'com 千锋官网']

join 拼接

In [100]: li = ['www', 'qfedu', 'com']
In [101]: url5 = ''.join(li)
In [102]: url6 = '_'.join(li)
In [103]: print(li)
In [104]: print(url5)
In [105]: print(url6)
Out[103]: ['www', 'qfedu', 'com']
Out[104]: wwwqfeducom
Out[105]: www_qfedu_com

replace 替换

In [106]: li = 'www.qfedu.com'
In [107]: url7 = li.replace('.', '_')
In [108]: print(url7)
Out[108]: www_qfedu_com

strip 移除两端的空格

s = ' hello   '
s2 = s.strip()

inp = input(">:").strip()
In [113]: s = "symbol=BCHBTC;baseCoin=BCH;quoteCoin=BTC;"
In [114]: s_list = s.split(';')
In [115]: print(s_list)
Out[115]: ['symbol=BCHBTC', 'baseCoin=BCH', 'quoteCoin=BTC', '']

startswith 判断字符串以什么为开头

s = 'hello world'
if s.startswith('h'):
    print(s)

endswith 判断字符串以什么为结尾

s = 'hello world'
if s.endswith('d'):
    print(s)

index 获取一个元素在字符串中的索引号

s = 'hello world'
idx = s.index('l')

image image image

交互输入

image

详细内容:https://www.jianshu.com/p/fabb57737176

相关文章

  • 1. 字符串

    创建 简单操作 \ 转义符 + 拼接 * 复制 字符串 和 0 或者 负数相乘,会得到一个空字符串 进阶操作 Py...

  • Java_字符串

    1.知识点: 不可变字符串 可变字符串 2.知识点运用: 1.不可变字符串: 1.字符串* 1. 不可变的字符串 ...

  • 2019-04-23总结

    1.字符串 1.字符串1.count(字符串2) --》统计字符串中字符串出现的次数 2.字符串1.find(...

  • 1.字符串下标

    # 字符串下标[起始位置 : 终止位置 : 间隔 :] say = "0123456789" print(say[...

  • Python语言基础之——字符串和函数基础

    1.字符串相关方法 1.计算次数 1.count 字符串1.count(字符串2) - 统计字符串1中...

  • 5.bytes和bytearray

    目录1.字符串与字节2.bytes3.bytearray 1.字符串与字节 1.1 字符串与bytes 字符串是字...

  • iOS开发中字典和字符串的相互转换

    OC: 1.字符串转字典 2.字典转字符串 Swift 1.//将数组/字典 转化为字符串 2. //将字符串转化...

  • iOS 字符串的相关处理简单总结

    NSString字符串处理:截取字符串、匹配字符串、分隔字符串 1.截取字符串 NSString*string =...

  • 牛客/力扣算法题

    1.逆转字符串 2.旋转字符串

  • R语言数据类型

    1.字符串 1.连接字符串 - paste()函数 语法说明 参数解释 2.格式化数字和字符串 - format(...

网友评论

      本文标题:1. 字符串

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