
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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.
- Related Articles
- Find the player who rearranges the characters to get a palindrome string first in Python
- How to find the first 10 characters of a string in C#?
- How can I get last 4 characters of a string in Python?
- Check whether second string can be formed from characters of first string in Python
- How to extract first two characters from a string in R?
- How to get first N characters from a MySQL column?
- How to get last 4 characters from string in\nC#?
- How to extract the first n characters from a string using Java?
- How to remove a list of characters in string in Python?
- How to get first day of the week using Python?
- Program to get final string after shifting characters with given number of positions in Python
- Python Program to Form a New String Made of the First 2 and Last 2 characters From a Given String
- How to get the length of a string in Python?
- How to get the size of a string in Python?
- Removing first k characters from string in JavaScript
