How to get first 100 characters of the string in Python?


In this article, we will show you how to get the first 100 characters of a string in python. The following are the 4 different methods to accomplish this task −

  • Using While Loop

  • Using For Loop

  • Using Slicing

  • Using slice() Function

Assume we have taken an input string. We will return the first 100 characters of the input string using the above-specified methods.

Method 1: Using While Loop

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input string.

  • Create another variable to store the number of characters count by initializing it with 0 first (as the index starts from 0).

  • Use a while loop for iterating until the index count is less than 100.

  • Inside the while loop, print the character of the string at the current index.

  • Incrementing the index count by 1 after every iteration.

Example

The following program returns the first 100 characters of the input string using the while loop −

# 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

Output

On executing, the above program will generate the following output −

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

Method 2: Using For Loop

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input string

  • Create a new empty string to store the result string.

  • Create another variable to store the number of characters count by initializing it with 0 first(as the index starts from 0).

  • Use the for loop, to traversing in each character of the input string.

  • Use the if conditional statement to check whether the iterator index value is less than 100.

  • Concatenate the corresponding character to the result string if the above condition is true.

  • Else break a loop(Going out from the loop).

  • Incrementing the index count by 1 after every iteration.

  • Print the resultant string which contains the first 100 characters of the input string.

Example

The following program returns the list of all the values of a dictionary using [] and * operator

# 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)

Output

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

Method 3: Using Slicing

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input string.

  • Get the first 100 characters of the input string using slicing and create a variable to store it.

  • Print the resultant string which contains the first 100 characters of the input string.

Example

The following program returns the first 100 characters of the input string using slicing −

# 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)

Output

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

Method 4: Using slice() Function

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input string.

  • Pass 100 as an argument to the slice() function as we require 100 characters.

  • Get the first 100 characters of the input string using the slice count value and create a variable to store it.

  • Print the resultant string which contains the first 100 characters of the input string.

Syntax

slice(start, end, step)

A slice object is returned by the slice() function.

A slice object specifies how to slice a sequence. You can specify where the slicing should begin and end. You can optionally define the step, which allows you to slice every other item.

Example

The following program returns the first 100 characters of the input string using slice() function −

# 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)

Output

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

Conclusion

In this article, we learned how to get the first 100 characters of a string. This method can also be used to get the first N characters of a string, list, tuple, and so on. We also learned how to slice a string with slicing and how to use the slice() function to implement slicing with the function.

Updated on: 19-Sep-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements