python字符串基础

字符串:用 ‘ ’ 或 “ ” 表示
创建字符串:直接赋值。school = ‘qcyn’ 或者 school=“qcyn”

一、字符串的基本操作

1.索引 (indexing)

下标从0开始

>>>name='zhouke'
>>>name[1]
>>>'1'

2.分片(slicing)

>>>name='zhouke'
>>>name[-1]
>>>'e'
>包前不包后
>>>name='zhouke'
>>>name[1:3]
>>>'ho'
>>> a = "abcdef"
 >>> a[:3]
 'abc'
 >>> a[::2]
 'ace'
 >>> a[5:1:2]
 ''
 >>> a[1:5:2]
 'bd'
 >>> a[::-2]
 'fdb'
 >>> a[5:1:-2]
 'fd'

3.序列相加(adding)

>>>n='zhouke'
>>>b='lihai'
>>>n+b
>'zhoukelihai'

4.乘法(multiplying)

>>>x='abcd'
>>>x*2
>'abcdabcd'

5.成员资格

>>>a='hello world'
>>>'o' in a
>True
>>>'p' in a
>False
>>>'k' not in a
>True

6.长度

>>>k='abcdefghi'
>>>len(k)
>9

7.最大值 最小值

In [1]: a='1312312319'

In [2]: print(max(a))
9

In [3]: b='poiuytrej'

In [4]: print(min(b))
e

你可能感兴趣的:(python字符串基础)