Python:字符串处理函数

Split & Combine  #拆分和组合

#split() 通过指定分隔符对字符串进行切片
lan = "python ruby c c++ swift"
lan.split()
[‘python‘, ‘ruby‘, ‘c‘, ‘c++‘, ‘swift‘]

todos = "download python, install, download ide, learn"
todos.split(‘, ‘)
[‘download python‘, ‘install‘, ‘download ide‘, ‘learn‘]

‘,‘.join([‘download python‘, ‘install‘, ‘download ide‘, ‘learn‘])
‘download python,install,download ide,learn‘

Substitue #替换

#replace()
s = ‘I like C. I like C++. I like Python‘
s.replace(‘like‘, ‘hate‘)
‘I hate C. I hate C++. I hate Python‘

s.replace(‘like‘, ‘hate‘, 1)
‘I hate C. I like C++. I like Python‘
Layout #布局
align = ‘Learn how to align‘
align.center(30)
‘      Learn how to align      ‘

align.ljust(30)
‘Learn how to align            ‘

align.rjust(30)
‘            Learn how to align‘

ralign = align.rjust(30)
ralign.strip()
‘Learn how to align‘

Other useful tools

py_desc = "Python description: Python is a programming language that lets you work quickly and integrate systems more effectively."
py_desc.startswith(‘Python‘)
True

py_desc.endswith(‘effectively.‘)
True

py_desc.find(‘language‘)
44

py_desc.isalnum()
False

py_desc.count("Python")
2

py_desc.strip(‘.‘)
‘Python description: Python is a programming language that lets you work quickly and integrate systems more effectively‘

py_desc.upper()
‘PYTHON DESCRIPTION: PYTHON IS A PROGRAMMING LANGUAGE THAT LETS YOU WORK QUICKLY AND INTEGRATE SYSTEMS MORE EFFECTIVELY.‘

py_desc.title()
‘Python Description: Python Is A Programming Language That Lets You Work Quickly And Integrate Systems More Effectively.‘

New style in Python 3.6

print(‘%s %s‘ % (‘one‘, ‘two‘))
print(‘{} {}‘.format(‘one‘, ‘two‘))
print(‘%d %d‘ % (1, 2))
print(‘{} {}‘.format(1, 2))
one two
one two
1 2
1 2

print(‘{1} {0}‘.format(‘one‘, ‘two‘))
two one

a = 5
b = 10
print(f‘Five plus ten is {a + b} and not {2 * (a + b)}.‘)
Five plus ten is 15 and not 30.

name = "Joshua"
question = "hello"
print(f"Hello, {name}! How‘s it {question}?")
Hello, Joshua! How‘s it hello?

原文地址:https://www.cnblogs.com/kumata/p/9052072.html

时间: 2024-08-30 17:57:11

Python:字符串处理函数的相关文章

python字符串操作函数和string模块代码分析

原文链接:http://blog.chinaunix.net/uid-25992400-id-3283846.html python的字符串属性函数 字符串属性方法: >>> str='string learn' >>> dir(str) ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute_

Python字符串format函数

python从2.6开始支持format,一种新的更加容易读懂的字符串格式化方法. 1. 替代旧的%输出 旧的格式化输出方法: #!/usr/bin/python name = 'Tom' age = 18 print '%s is %d years old' % (name,age) 使用format函数格式化输出: #!/usr/bin/python name = 'Tom' age = 18 print '{0} is {1} years old'.format(name,age) 相比于

Python 字符串操作函数

#-*- coding:utf-8 -*- strword = "i will fly with you , fly on the sky ." #find print(strword.find("fly")) #打印7 #find返回找到第一个字符串的下标,找不到返回-1 print("==rfind==") #rfind print(strword.rfind("fly")) #rfind 从后向前查找,找到返回下标,找不

python 字符串常用函数有哪些?

声明变量str="Hello World"find() 检测字符串是否包含,返回该字符串位置,如果不包含返回-1str.find("Hello") # 返回值:0str.find("W") # 返回值:6, 这里需要注意下:空格也是一个字符.W前面有个空格,所以W位置是6str.find("R") # 返回值:-1,并不包含在Hello World中,如果不包含返回-1index() 检测字符串是否包含指定的字符,并返回开始的

Python字符串处理函数

返回被去除指定字符的字符串 默认去除空白字符 删除首尾字符:str.strip([char]) 删除首字符:str.lstrip([char]) 删除尾字符str.strip([char]) 判断是否匹配首末字符 匹配成功返回True,否则返回False 匹配首字符:str.startswith(char[, start[, end]]) 匹配末字符:str.endswith(char[, start[, end]]) 查找字符,找到返回字符位置,否则返回-1 从字符串开头查找str.find(

<整理> Python 字符串常用函数

来源:https://www.cnblogs.com/sshcy/p/8065113.html 1 str.replace(old, new[, max]) 参数含义: old: 原文地址:https://www.cnblogs.com/icemaster/p/10344399.html

[摘抄]Python内置的字符串处理函数整理

str='python String function' 生成字符串变量str='python String function' 字符串长度获取:len(str)例:print '%s length=%d' % (str,len(str)) 字母处理全部大写:str.upper()全部小写:str.lower()大小写互换:str.swapcase()首字母大写,其余小写:str.capitalize()首字母大写:str.title()print '%s lower=%s' % (str,st

python字符串函数

#连接字符串 sStr1 = 'strcat'sStr2 = 'append'sStr1 += sStr2print(sStr1) #复制字符串sStr1 = 'strcpy'sStr2 = sStr1sStr1 = 'strcpy2'print(sStr2) #比较字符串#strcmp(sStr1,sStr2)sStr1 = 'strchr'sStr2 = 'strch'print(sStr1 == sStr2)#注意cmp()在python3中移除了! #截取字符串#特别注意:下标从0开始:

【python】format函数格式化字符串的用法

来源:http://www.jb51.net/article/63672.htm 自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式化字符串相比,有什么优越的存在呢?让我们来揭开它羞答答的面纱.语法 它通过{}和:来代替%.“映射”示例 通过位置 ? 1 2 3 4 5 6 In [1]: '{0},{1}'.format('kzc',18) Out[1]: 'kzc,18' In [2]: '{},{}'.format('kz

Python 的格式化字符串format函数

阅读mattkang在csdn中的博客<飘逸的python - 增强的格式化字符串format函数>所做笔记 自从python2.6开始,新增了一种格式化字符串的函数str.format(),他通过{}和:来代替%. 1.映射实例 In[1]: '{0},{1}'.format('abc', 18) Out[1]: 'abc,18' In[2]: '{}'.format(18) out[2]: 18 In[3]: '{1},{0},{1}'.format('abc', 123) out[3]: