如何在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個字元,所以將100作為引數傳遞給**slice()**函式。

  • 使用切片計數值獲取輸入字串的前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()函式來實現使用該函式的切片。

更新於:2022年9月19日

5000+ 次檢視

啟動您的職業生涯

完成課程後獲得認證

開始
廣告
© . All rights reserved.