如何在 Python 中获取字符串的前 100 个字符?

pythonserver side programmingprogramming更新于 2025/5/4 18:20:17

在本文中,我们将向您展示如何在 Python 中获取字符串的前 100 个字符。以下是完成此任务的 4 种不同方法 -

  • 使用 While 循环

  • 使用 For 循环

  • 使用切片

  • 使用slice() 函数

假设我们已经获取了一个输入字符串。我们将使用上面指定的方法返回输入字符串的前 100 个字符。

方法 1:使用 While 循环

算法(步骤)

以下是执行所需任务需要遵循的算法/步骤 -

  • 创建一个变量来存储输入字符串。

  • 创建另一个变量来存储字符数,方法是先将其初始化为 0(因为索引从 0 开始)。

  • 使用 while 循环进行迭代,直到索引计数小于 100。

  • 在 while 循环内,打印当前索引处的字符串字符。

  • 每次之后将索引计数增加 1迭代。

示例

以下程序使用 while 循环返回输入字符串的前 100 个字符 -

# input string inputString = "Hello everyone welcome to the tutorialsPoint python sample codes Hello everyone welcome to the tutorialsPoint python sample codes Hello everyone welcome to the tutorialsPoint python sample codes" # storing the character's count indexCount=0 # Using while loop for iterating until the index count is less than 100 while indexCount <100: # printing the character of the string at the current index print(inputString[indexCount], end="") # incrementing the index count by 1 indexCount += 1

输出

执行时,上述程序将生成以下输出 -

Hello everyone welcome to the tutorialsPoint python sample codes Hello everyone welcome to the tutor

方法 2:使用 For 循环

算法(步骤)

以下是执行所需任务所要遵循的算法/步骤 -

  • 创建一个变量来存储输入字符串

  • 创建一个新的空字符串来存储结果字符串。

  • 创建另一个变量来存储字符数,方法是先将其初始化为 0(因为索引从 0 开始)。

  • 使用 for 循环遍历输入字符串的每个字符。

  • 使用 if 条件语句检查迭代器索引值是否小于 100。

  • 如果上述条件为真,则将相应的字符连接到结果字符串。

  • 否则打破循环(退出循环)。

  • 每次迭代后将索引计数增加 1。

  • 打印包含输入字符串前 100 个字符的结果字符串。

示例

以下程序使用 [] 和 * 运算符返回字典中所有值的列表

# input string inputString = "Hello everyone welcome to the tutorialsPoint python sample codes Hello everyone welcome to the tutorialsPoint python sample codes Hello everyone welcome to the tutorialsPoint python sample codes" # resultant string resultant_string = "" # storing the characters count(index) indexCount = 0 # Traversing in each character of the input string for character in inputString: # Checking whether the iterator index value is less than 100 if indexCount <100: # concatenating the corresponding character to the result string resultant_string += character else: break # incrementing the index count by 1 indexCount = indexCount + 1 # printing the first 100 characters of the string print(resultant_string)

输出

Hello everyone welcome to the tutorialsPoint python sample codes Hello everyone welcome to the tutor

方法 3:使用切片

算法(步骤)

以下是执行所需任务需要遵循的算法/步骤 -

  • 创建一个变量来存储输入字符串。

  • 使用切片获取输入字符串的前 100 个字符并创建一个变量来存储它。

  • 打印包含输入字符串前 100 个字符的结果字符串。

示例

以下程序使用切片返回输入字符串的前 100 个字符 -

# input string inputString = "Hello everyone welcome to the tutorialsPoint python sample codes Hello everyone welcome to the tutorialsPoint python sample codes Hello everyone welcome to the tutorialsPoint python sample codes" # getting first 100 characters of the input string using slicing resultant_string = inputString[:100] # printing the resultant string print(resultant_string)

输出

Hello everyone welcome to the tutorialsPoint python sample codes Hello everyone welcome to the tutor

方法 4:使用slice()函数

算法(步骤)

以下是执行所需任务所要遵循的算法/步骤 -

  • 创建一个变量来存储输入字符串。

  • 将 100 作为参数传递给 slice() 函数,因为我们需要 100 个字符。

  • 使用切片计数值获取输入字符串的前 100 个字符并创建一个变量来存储它。

  • 打印包含输入字符串前 100 个字符的结果字符串。

语法

slice(start, end, step)

slice 对象由slice() 函数。

slice 对象指定如何对序列进行切片。您可以指定切片的开始和结束位置。您可以选择定义步骤,以便对每个其他项目进行切片。

示例

以下程序使用slice() 函数返回输入字符串的前 100 个字符 -

# input string inputString = "Hello everyone welcome to the tutorialsPoint python sample codes, lets start coding and understand how Slicing works in Python" # passing 100 as an argument to the slice() function as we require 100 characters slice_count = slice(100) # getting first 100 characters of the input string using slice() function resultant_string = inputString[slice_count] # printing the resultant string print(resultant_string)

输出

Hello everyone welcome to the tutorialsPoint python sample codes Hello everyone welcome to the tutor

结论

在本文中,我们学习了如何获取字符串的前 100 个字符。此方法还可用于获取字符串、列表、元组等的前 N ​​个字符。我们还学习了如何使用切片对字符串进行切片以及如何使用切片() 函数实现切片。


相关文章


有用资源