Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-如何在索引中使用字符串_Python_Python 3.x - Fatal编程技术网

Python-如何在索引中使用字符串

Python-如何在索引中使用字符串,python,python-3.x,Python,Python 3.x,我想知道如何使用等于整数的字符串作为索引中的整数 word = input("Enter word:") print(word) letterNum = int(len(word)) #determines amount of letters in word print(letterNum) lastLetter = word[letterNum] #supposed to figure out the last letter in a word print(lastLetter) 这将为您提

我想知道如何使用等于整数的字符串作为索引中的整数

word = input("Enter word:")
print(word)
letterNum = int(len(word)) #determines amount of letters in word
print(letterNum)
lastLetter = word[letterNum] #supposed to figure out the last letter in a word
print(lastLetter)

这将为您提供一个单词中没有所有代码的最后一个字母。不过,我不确定你在问什么

word = input("Enter word: ")
print(word[-1])
例如:

Enter word: Test
#"t"
如果您询问是否输入了
“Test 1”
,并且希望将最后一个字符作为一个数字,则只需将其包装在
int
中即可,但请先进行一些检查

word = input("Enter word: ")
last_char = word[-1]

if isnumeric(word[-1]):
    print(int(last_char))
else:
    print(last_char)
示例:

Enter word: Test 1
#1

Enter word: Test
#"t"

这将为您提供一个单词中没有所有代码的最后一个字母。不过,我不确定你在问什么

word = input("Enter word: ")
print(word[-1])
例如:

Enter word: Test
#"t"
如果您询问是否输入了
“Test 1”
,并且希望将最后一个字符作为一个数字,则只需将其包装在
int
中即可,但请先进行一些检查

word = input("Enter word: ")
last_char = word[-1]

if isnumeric(word[-1]):
    print(int(last_char))
else:
    print(last_char)
示例:

Enter word: Test 1
#1

Enter word: Test
#"t"

python最简单的方法是使用
-1
索引。负索引从字符串末尾开始计数,因此
word[-1]
将始终为您提供最后一个字母使用python最简单的方法是使用
-1
进行索引。负索引从字符串末尾开始计数,因此
word[-1]
将始终为您提供最后一个字母

这里我将为您提供上面包含的几个示例

word = input("Enter word:").strip() # strip() is used to remove any trailing or leading white spaces
print(word)
letterNum = int(len(word)) # Determines amount of letters in word
print(letterNum)

# 1st way (Using the internet that we created above) 
lastLetter = word[letterNum - 1] # Supposed to figure out the last letter in a word
print(lastLetter)

# 2nd way (As the above answers suggest, -ve index, -1 for last, -2 for 2nd last, this is best, but this basically for Python, other language like C/C++ etc. use 1st way) 
print(word[-1]) 

# 3rd way (Not good, but it is good for those who are learning Python,Reversing the string and printing 1st character) 
print(word[::-1][0]) 

这里我给你们举几个例子,包括上面的例子

word = input("Enter word:").strip() # strip() is used to remove any trailing or leading white spaces
print(word)
letterNum = int(len(word)) # Determines amount of letters in word
print(letterNum)

# 1st way (Using the internet that we created above) 
lastLetter = word[letterNum - 1] # Supposed to figure out the last letter in a word
print(lastLetter)

# 2nd way (As the above answers suggest, -ve index, -1 for last, -2 for 2nd last, this is best, but this basically for Python, other language like C/C++ etc. use 1st way) 
print(word[-1]) 

# 3rd way (Not good, but it is good for those who are learning Python,Reversing the string and printing 1st character) 
print(word[::-1][0]) 

有没有办法在那里使用字符串?有没有办法在那里使用字符串?你能举个例子说明“作为索引中的整数的等于整数的字符串”是什么意思吗?你能举个例子说明“作为索引中的整数的等于整数的字符串”是什么意思吗?有没有办法使用变量作为索引但是整数?@Turtle是的,就在我打印它的地方放
variable=int(last\u char)
但是在数组索引中,比如如果我把num=2,我有没有办法做到word1[num],word2[num],word3[(num-1)],等等!这绝对是合法使用。请看伟大的答案,以更好地了解我和@kayes的答案中发生了什么。但是,只要num在
0--len(word)-1
之内,那么
word[num]
就会在字符串中返回该字符。但是有没有办法将变量用作整数呢?@Turtle是的,就在我打印它的地方,只要把
variable=int(last\char)
放在数组索引中,比如,如果我把num=2,有没有一种方法可以让我绝对地做到word1[num],word2[num],word3[(num-1)],等等!这绝对是合法使用。请看伟大的答案,以更好地了解我和@kayes的答案中发生了什么。但是只要num在
0--len(word)-1
之内,那么
word[num]
将在字符串中返回该字符