你做了什么错:
你在哪里试图做的是创造什么,其唯一的元素是一个字符串列表。当它是一个列表时,forename[0]
将采用该列表中的第一个(也是唯一)元素(就像该字符串是从input()
直接获取的那样),但不是来自该字符串。
如何解决此问题:
没有必要将它转化成列表,切片标志允许使用:
forename = input("Forename: ")
forenameFirstLetter = forename[0]
所以,现在是不必要后来转换为字符串:
email = forenameFirstLetter + "." + surname + "@TreeRoad.net"
print ("This is the students email address:" + email)
到u nderstand更好的切片字符串:
0 | 1 | 2 | 3 | (index)
f | o | o | . | (string)
当你切的字符串:
s = "foo."
s[0] #is "f" because it corresponds with the index 0
s[1] #is "o"
s[2] #is "o"
s[0:2] #takes the substring from the index 0 to 2. In this example: "foo"
s[:1] #From the start of the string until reaching the index 1. "fo"
s[2:] #From 2 to the end, "o."
s[::2] #This is the step, here we are taking a substring with even index.
s[1:2:3] #You can put all three together
所以语法string[start:end:step]
。
用于列表非常相似。
非常感谢你 –