Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get first 100 characters of the string 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 using square brackets [] with numbers inside to specify the range.
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 Slicing
Slicing is the most efficient and Pythonic method for extracting a portion of a string. In Python, slicing is done using the colon operator :. The syntax string[:n] returns the first n characters ?
Syntax
string[:n] # Get first n characters
Example
# input string
text = "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 using slicing
first_100_chars = text[:100]
# printing the result
print(first_100_chars)
print(f"Length: {len(first_100_chars)}")
Hello everyone welcome to the tutorialsPoint python sample codes Hello everyone welcome to the tutor Length: 100
Using slice() Function
The slice() function creates a slice object that can be used to extract characters from a string. This approach is useful when you need to reuse the same slice pattern multiple times ?
Syntax
slice(start, stop, step)
Example
# input string
text = "Hello everyone welcome to the tutorialsPoint python sample codes, lets start coding and understand how Slicing works in Python"
# creating a slice object for first 100 characters
slice_obj = slice(100)
# applying the slice to get first 100 characters
first_100_chars = text[slice_obj]
# printing the result
print(first_100_chars)
print(f"Length: {len(first_100_chars)}")
Hello everyone welcome to the tutorialsPoint python sample codes, lets start coding and understand h Length: 100
Using While Loop
A while loop can iterate through each character of the string until we reach the 100th character. This method is less efficient but demonstrates manual character extraction ?
Example
# input string
text = "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"
# variables for loop control
result = ""
index = 0
# using while loop to get first 100 characters
while index < 100 and index < len(text):
result += text[index]
index += 1
# printing the result
print(result)
print(f"Length: {len(result)}")
Hello everyone welcome to the tutorialsPoint python sample codes Hello everyone welcome to the tutor Length: 100
Using For Loop
A for loop with enumeration can also extract the first 100 characters by tracking the character position and breaking when we reach the limit ?
Example
# input string
text = "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"
# result string
result = ""
# using for loop with enumerate to get first 100 characters
for index, char in enumerate(text):
if index < 100:
result += char
else:
break
# printing the result
print(result)
print(f"Length: {len(result)}")
Hello everyone welcome to the tutorialsPoint python sample codes Hello everyone welcome to the tutor Length: 100
Handling Edge Cases
When working with strings shorter than 100 characters, all methods will simply return the entire string without errors ?
# short string example
short_text = "Hello Python!"
# all methods work safely with short strings
print("Slicing:", short_text[:100])
print("Length:", len(short_text[:100]))
Slicing: Hello Python! Length: 13
Performance Comparison
| Method | Performance | Readability | Best Use Case |
|---|---|---|---|
Slicing [:100]
|
Fastest | Excellent | General use |
slice() function |
Fast | Good | Reusable patterns |
| While loop | Slow | Poor | Learning purposes |
| For loop | Slow | Poor | Learning purposes |
Conclusion
The slicing method string[:100] is the most efficient and Pythonic way to get the first 100 characters of a string. Use slice() function when you need reusable slice patterns, while loops are mainly useful for educational purposes.
