[course]04 —— String

Learn Python3 The Hard Way

EX6

格式化字符串 Format String

types_of_people = 10 
x = f"There are {types_of_people} types of people."

binary = "binary" 
do_not = "don't" 
y = f"Those who know {binary} and those who {do_not}."

print(x) 
print(y)

print(f"I said: {x}") 
print(f"I also said: '{y}'")

hilarious = False 
joke_evaluation = "Isn't that joke so funny?! {}"

print(joke_evaluation.format(hilarious)) w = "This is the left side of..."

e = "a string with a right side."

print(w + e)

EX7

print方法及其参数

EX8

format string变量

EX9

Escape sequence 转义字符串

String Literals

1. 四种类型的字符串

2. 换行的几种方式

  1. \n 转义字符

  2. 多行字符串 """'''

3. More Escape Sequences

4. An escape sequence produces a single character:

5. repr() 和 print() 方法

6. 多行注释

2. String常量

3. String 操作符

1. String 的 加号 和 乘号

2. in 操作符

String的in操作符: in操作符用于查找是否存在子字符串

3. String indexing and slicing

1. Indexing a single character

2. Negative indexes

3. string 切片

4. 切片的默认参数

5. 切片的步长参数 step

6. 字符串反转

使用切片进行字符串反转

4. Looping over Strings

1. "for" 循环索引字符串

2. "for" loop without indexes

3. "for" loop with split

4. "for" loop with splitlines

5. Example: isPalindrome

A string is a palindrome if it is exactly the same forwards and backwards.

**6. Strings are

1. 字符串是不可以修改的,不要修改字符串

2. 要修改字符串应该重新创建一个字符串

7. 字符串的默认方法

1. str() and len()

2. chr() and ord()

3. eval()

8. String方法

Methods are a special type of function that we call "on" a value, like a string. You can tell it's a method because the syntax is in the form of value.function(), like s.islower() in the code below.

1. Character types: isalnum(), isalpha(), isdigit(), islower(), isspace(), isupper()

2. String edits: lower(), upper(), replace(), strip()

3. Substring search: count(), startswith(), endswith(), find(), index()

9. String Formatting

1. format a string with %s

2. format an integer with %d

3. format a float with %f

4. format a float with %.[precision]f

You can control how many fractional digits of a float are included in the string by changing the number to the right of the decimal point.

5. format multiple values

6. format right-aligned with %[minWidth]

7. format left-aligned with %-[minWidth]

8. String Formatting with f Strings

10. Basic File IO

文件操作

Last updated

Was this helpful?