Python | First N letters string construction
Last Updated :
16 May, 2023
Sometimes, rather than initializing the empty string, we need to initialize a string in a different way, vis., we may need to initialize a string with 1st N characters in English alphabets. This can have applications in competitive Programming. Let’s discuss certain ways in which this task can be performed.
Method #1: Using join() + list comprehension This task can be performed with the combination of the above functions. The join function can be used to join the string and list comprehension can perform the task of the logic of adding N numbers.
Python3
N = 15
res = ' '.join([' % c' % x for x in range ( 97 , 97 + N)])
print ( "The string after construction : " + str (res))
|
Output :
The string after construction : abcdefghijklmno
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using ord() + join() + list comprehension This is yet another way in which this task can be performed, the ord function performs the task of type casting of ascii number to a character, rest technique is similar to above method.
Python3
N = 15
res = ' '.join(chr(ord(' a') + i) for i in range (N))
print ( "The string after construction : " + str (res))
|
Output :
The string after construction : abcdefghijklmno
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using Slicing
Python3
letters = "abcdefghijklmnopqrstuvwxyz"
N = 15
res = letters[:N]
print ( "The string after construction : " + str (res))
|
Output
The string after construction : abcdefghijklmno
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4:Using the bytearray() function.
Step-by-step approach:
- Initialize the value of N.
- Create a range from 97 to 97+N using the range() function.
- Convert the range to a bytearray using the bytearray() function.
- Convert the bytearray to a string using the str() function and specifying the encoding as ‘utf-8’.
- Print the result.
Python3
N = 15
res = str (bytearray( range ( 97 , 97 + N)), 'utf-8' )
print ( "The string after construction : " + str (res))
|
Output
The string after construction : abcdefghijklmno
Time Complexity: O(N).
Auxiliary Space: O(N).
Method #5:Using reduce() and join():
Step-by-step approach:
- Set the value of N to the desired length of the string.
- Use a list comprehension to create a list of characters from ‘a’ to ‘a’ + N – 1.
- Use reduce() to concatenate the characters in the list into a single string.
- Print the resulting string.
Python3
from functools import reduce
N = 15
char_list = [ chr ( ord ( 'a' ) + i) for i in range (N)]
res = reduce ( lambda x, y: x + y, char_list)
print ( "The string after construction: " + str (res))
|
Output
The string after construction: abcdefghijklmno
Time Complexity: O(N).
Auxiliary Space: O(N).
Method #6: Using itertools.islice()
Step-by-step approach:
- Import the itertools module.
- Define the letters string as given in the problem statement.
- Initialize the value of N.
- Call the islice() function with the letters string as the first argument, and N as the second argument. This returns an iterator object that yields the first N characters of letters.
- Join the characters obtained in step 4 using the join() function to get the final string.
- Print the final string.
Python3
import itertools
letters = "abcdefghijklmnopqrstuvwxyz"
N = 15
res = ''.join(itertools.islice(letters, N))
print ( "The string after construction : " + str (res))
|
Output
The string after construction : abcdefghijklmno
Time complexity: O(N) where N is the number of characters to be extracted from the letters string.
Auxiliary space: O(N) to store the resulting string.
Share your thoughts in the comments
Please Login to comment...