python3基础语法——对String基本操作(创建/切割/连接)

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语句的灵活应用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值