Python:字符串处理函数
Posted KumataのBloG
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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?
以上是关于Python:字符串处理函数的主要内容,如果未能解决你的问题,请参考以下文章
你如何在 python 中处理 graphql 查询和片段?
Python代码阅读(第38篇):根据谓词函数和属性字符串构造判断函数