如何在 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() 函式使用函式實現切片。

更新於: 2022年9月19日

5K+ 次瀏覽

啟動你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.