美文网首页
Python3基础进阶(二)

Python3基础进阶(二)

作者: chenzf2017 | 来源:发表于2017-05-28 21:46 被阅读0次

3.8·Get Length with len()

The len() function counts characters in a string:

>>> letters = 'abcdefg'

>>> len(letters)

7

3.9·Split with split()

Some functions are specific to strings. To use a string function, type thename of the string, a dot, the name of the function, and anyargumentsthat the function needs:

string . function ( arguments )

You can use the built-in stringsplit()function tobreak a string into a listof smaller stringsbased on someseparator.

>>> letters = 'abc,def,ghi'

>>> letters.split(',')

['abc', 'def', 'ghi']

In the preceding example, the string was calledlettersand the string function was called split(), with the single separator argument ','.If you don’t specify a separator, split() uses any sequence of white space characters—newlines, spaces, and tabs.

>>> letters = 'a b c d,ef gh,i'

>>> letters.split()

['a', 'b', 'c', 'd,ef', 'gh,i']

3.10·Combine with join()

Thejoin()function is the opposite of split(): it collapses alistof stringsintoa singlestring.

>>> crypto_list = ['a', 'b', 'c']

>>> crypto_string = '-'.join(crypto_list)

>>> crypto_string

'a-b-c'

3.11·Playing with Strings

Python has a large set ofstring functions. Let’s explore how the most common of them work.

>>> poem = '''All that doth flow we cannot liquid name

Or else would fire and water be the same;

But that is liquid which is moist and wet

Fire that property can never get.

Then 'tis not cold that doth the fire put out

But 'tis the wet that makes it die, no doubt.'''

Does it start with the letters All?

>>> poem.startswith('All')

True

Does it end withThat's all, folks!?

>>> poem.endswith('That\'s all, folks!')

False

let’s find theoffsetof the first occurrence of the wordthein the poem:

>>> poem.find('the')

73

And the offset of the lastthe:

>>> poem.rfind('the')

214

How many times does the three-letter sequencetheoccur?

>>> poem.count('the')

3

Are all of the characters in the poemeither letters or numbers?

>>> poem.isalnum()

False

Nope, there were some punctuation characters.

3.12·Case and Alignment

In this section, we’ll look at some more uses of thebuilt-in string functions.

Our test string is the following:

>>> setup = '...a ..duck goes into a bar...'

>>> setup.strip('.')

'a ..duck goes into a bar'    # Remove . sequences from both ends:

Becausestrings are immutable, none of these examples actually changes the setup string. Each example justtakes the value of setup, does something to it, andreturns the result as a new string.

>>> setup = 'a duck goes into a bar...'

>>> setup.capitalize()

'A duck goes into a bar...'

>>> setup.title()

'A Duck Goes Into A Bar...'

>>> setup.upper()

'A DUCK GOES INTO A BAR...'

>>> setup.lower()

'a duck goes into a bar...'

>>> setup = 'a duck goes into a Bar...'

>>> setup.swapcase()

'A DUCK GOES INTO A bAR...'

Now, we’ll work with somelayout alignment functions.

>>> setup = 'a duck goes into a Bar...'

>>> setup.center(30)

'  a duck goes into a Bar...  '

>>> setup = 'a duck goes into a Bar...'

>>> setup.ljust(30)

'a duck goes into a Bar...    '

>>> setup.rjust(30)

'    a duck goes into a Bar...'

3.13·Substitute with replace()

You usereplace()for simplesubstring substitution. You give it the old substring, the new one, andhow many instances of the old substring to replace. If you omit this final count argument, it replaces all instances.

>>> setup = 'a b c a b c a'

>>> setup.replace('a', 'd', 2)

'd b c d b c a'

Sometimes, you want to ensure that the substring is a whole word, or the beginning of a word, and so on. In those cases, you needregular expressions.

2017.5.27

Chapter 3. Py Filling: Lists, Tuples, Dictionaries, and Sets

In Chapter 2 we started at the bottom with Python’s basic data types:booleans, integers, floats, and strings. If you think of those as atoms, the data structures in this chapter are like molecules.

1.1·Lists and Tuples

相关文章

  • Python3基础进阶(二)

    3.8·Get Length with len() The len() function counts chara...

  • [Python] Python学习资料

    更新中... Python 基础 廖雪峰的Python教程 Python3 中文手册 Python 进阶 Pyth...

  • 前端基础进阶系列

    前端基础进阶(一):内存空间详细图解前端基础进阶(二):执行上下文详细图解前端基础进阶(三):变量对象详解前端基础...

  • Python3基础知识

    Python3基础知识 | 基础语法 Python3基础知识 | 编程第一步 Python3基础知识 | 基本数据...

  • Python入门系列(二)——基础

    此章节过一遍python3基础,方便日后回忆。目录:一、python3保留字二、python3数据类型三、基本逻辑...

  • java web目录

    java web目录 web编程基础web编程进阶(一)web编程进阶(二)web编程原理

  • 技术栈

    一、HTML、CSS基础、JavaScript语法基础。 二、JavaScript语法进阶。包括:作用域和闭包、t...

  • 2020前端技术栈

    一、HTML、CSS基础、JavaScript语法基础。二、JavaScript语法进阶。包括:作用域和闭包、th...

  • express入门

    基础 进阶

  • Outing 组织清单

    (一)Outing组织基础 (二)Outing 组织进阶——时间管理与私人定制

网友评论

      本文标题:Python3基础进阶(二)

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