String 基础内容
1. 创建和打印
my_name = "Peter"
print (my_name)
----------------
Peter
"""
可以换航
"""
2. 可以当成一个列表,其中列表相关的基础操作详见,list小节
// A string can be thought of as a list of characters
my_name = "Peter"
first_initial = my_name[0]
3. Cut Me a Slice of String 切割字符串
// string_name[first_index:last_index]starts at (and includes) the first_index and ends at (but excludes) the last_index
first_name = "Rodrigo"
last_name = "Villanueva"
new_account=last_name[:5]
temp_password= last_name[2:6]
4. Concatenating Strings 连接两个字符串
// favorite_fruit = fruit_prefix + fruit_suffix
// fruit_sentence = "My favorite fruit is " + favorite_fruit
5. 取字符串长度-----len()方法
6. 字符串的负指数
// Negative Indices
favorite_fruit = 'blueberry`
favorite_fruit[-1]
favorite_fruit[-2]
favorite_fruit[-3:]
------------------------
'y'
'r'
'rry'
7.字符串是不可变的 Strings are Immutable, 但是可以连接字符串
//This means that we cannot change a string once it is created.
//And we can’t change an individual character.
first_name = "Bob"
first_name[0] = "R"
------------------------
Traceback (most recent call last):
File "script.py", line 4, in <module>
first_name[0] = "R"
TypeError: 'str' object does not support item assignment
//password_generator create a for loop that iterates through the indices username by going from 0 to len(username). To shift the letters right, at each letter the for loop should add the previous letter to the string password
def password_generator(user_name):
password = ""
for i in range(0, len(user_name)):
password += user_name[i-1]
return password
8. 使用 \ 将在一个字符串中的特殊字符进行转译
// favorite_fruit_conversation = "He said, "blueberries are my favorite!""
favorite_fruit_conversation = "He said, \"blueberries are my favorite!\""
9. 遍历字符串 iterating through strings
// counting a certain letter in a word
// letter in word or word in word-----使用 **in** . letter in word is a boolean expression that is True if the string letter is in the string word.
"e" in "blueberry"
"blue" in "strawberry"
-----------------------
True
False
// Write a function called contains that takes two arguments, big_string and little_string and returns True if big_string contains little_string
def contains(big_string,little_string):
if little_string in big_string:
return True
else:
return False
def contains(big_string, little_string):
return little_string in big_string
# letter in word 原本就是布尔类型
// Write a function called common_letters that takes two arguments, string_one and string_two and then returns a list with all of the letters they have in common
def common_letters(string_one,string_two):
common_lst=[]
new_string_one=[]
for new_letter_one in string_one:
if new_letter_one in new_string_one:
continue
new_string_one.append(new_letter_one)
for letter_in_one in new_string_one:
if letter_in_one in string_two:
common_lst.append(letter_in_one)
return common_lst
def common_letters(string_one, string_two):
common = []
for letter in string_one:
if (letter in string_two) and not (letter in common):
common.append(letter)
return common
# 首先要强调的是continue的使用
# 然后要注意,if语句的灵活应用