如何在 Python 中获取字符串的前 100 个字符?
在本文中,我们将向您展示如何在 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() 函数返回输入字符串的前 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 个字符。我们还学习了如何使用切片对字符串进行切片,以及如何使用 slice() 函数使用函数实现切片。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP