今天主要梳理了一下Python string的基本知识,一起来学习一下吧。
String is an immutable sequence of Unicode characters
replace(old, new, max)
max: 可以选参数,指执行替换次数,不带参数默认全替换。
str_1 = 'this is a string example...wow!!! this is really string'
print(str_1.replace('is', 'was'))
print(str_1.replace('is', 'was', 3))
# output:
thwas was a string example...wow!!! thwas was really string
thwas was a string example...wow!!! thwas is really string
Wrapping Strings with Quotes
Single quotes: ’
Double quotes: "
Triple doubles: “”"
Triple singles: ‘’’
普通的str,上面几种没啥区别,输出都是一样的
single_quote = 'hello world'
double_quote = "hello world"
triple_single = '''hello world'''
triple_double = """hello world"""
print(single_quote)
print(double_quote)
print(triple_single)
print(triple_double)
# output:
hello world
hello world
hello world
hello world
当字符串本身含有一些引号字符,为了避免歧义,可以采用其它方式来表达,如用不同与内容引号的其它类型包裹(含单引号就用双引号,含双引号就用单引号,含单引号和双引号就用三引号),或则是用转义字符 , ‘’’ 和 “”"可以用于原样输出
str1 = 'hello my" world'
str2 = 'hello my \' world'
str3 = "hello my''' world"
str4 = "hello my \" world"
str5 = '''hello ' world'''
str6 = '''hello \''' world'''
str7 = """hello
\t world"""
str8 = '''hello
world'''
print(f'str1: {str1}')
print(f'str2: {str2}')
print(f'str3: {str3}')
print(f'str4: {str4}')
print(f'str5: {str5}')
print(f'str6: {str6}')
print(f'str7: {str7}')
print(f'str8: {str8}')
# output:
str1: hello my" world
str2: hello my ' world
str3: hello my''' world
str4: hello my " world
str5: hello ' world
str6: hello ''' world
str7: hello
world
str8: hello
world
Strings: Common Methods and Operations
Operation | Description |
---|---|
upper() | Change string to uppercase |
lower() | Change string to lowercase |
title() | Display each word in titlecase |
rstrip(), lstrip(), strip() | Strip extra whitespaces |
tab (\t) | Add extra whitespaces |
concatenation (+) | Concatenate multiple strings |
split() | Break the string and return new list of strings |
str() | Convert Python object into string objects |
find() | Return the lowest index of the substring, or return -1 if nothing is found |
num = 3
new_str = 'python programming language'
print (new_str.title() + " has " + str(num) + " words!")
print (new_str.split(' '))
print (new_str.find('language'))
# output:
Python Programming Language has 3 words!
['python', 'programming', 'language']
19
lstrip: 去除字符串前面的空格
rstrip: 去除字符串后面的空格
strip: 去除字符串前后空格
str_test = " hello "
print(str_test.lstrip()) # output: "hello "
print(str_test.rstrip()) # output: " hello"
print(str_test.strip()) # output: "hello"
Random Access and Slicing
slice = string [ start: end]
slice = string [ start: end: step]
access = string[index]
str_object = 'python'
print(str_object[0:3]) # output:pyt
print(str_object[:3]) # output:pyt
print(str_object[3:]) # output:hon
print(str_object[-1]) # output:n
print(str_object[::-1]) # output:nohtyp
print(str_object[1::2]) # output:yhn
print(str_object[::2]) # output:pto
Sequence operations that apply to string
有关join()会在下一篇中介绍
long string can be continued wiht \ 多行显示
str1 = "python is a \
cool programing language"
print(str1)
# output:
python is a cool programing language
strings may be replicated with * 复制
str1 = 'python '
print(str1 * 3)
# output:
python python python
作业,答案也附上,哈哈!