Get First 100 Characters in Python



As you may know a string is a group of letters, numbers, or symbols in Python. Sometimes we do not need the whole string. We may only want a small part of it. For example, we may want only the first 100 characters. Python makes this easy by using something called slicing. Slicing means taking a part of the string. We use square brackets [] with numbers inside to do this. It is a very useful and simple way to get the part of a string that we need.

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 -

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

Using a While Loop

In Python the while loop is basically used to traverse over a block of code if the condition is true. The while loop can also be used to traverse over a string and return the first 100 characters.

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 running, the above program will generate the following output -

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

Using a For Loop

In Python the for loop is basically used to iterate over a sequence, it can be a list, tuple, dictionary, set or string. The for loop can also be used to traverse over a string and return the first 100 characters.

Example

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

# 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

Using Slicing

Slicing is a method of getting a portion of a sequence. In Python, slicing is done using the colon operator (:). The slice operator can also be used to get the first 100 characters of a 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

Using slice() Function

The slice() function is basically a built-in function in Python. This function returns a slice object. The slice object is used to slice the string, list, tuple, etc.

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: 2025-04-17T18:59:18+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements