Open In App

Python | First N letters string construction

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 code to demonstrate
# First N letters string construction
# using join() + list comprehension
 
# initializing N
N = 15
 
# using join() + list comprehension
# First N letters string construction
res = ''.join(['% c' % x for x in range(97, 97 + N)])
     
# print result
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 code to demonstrate
# First N letters string construction
# using join() + list comprehension + ord()
 
# initializing N
N = 15
 
# using join() + list comprehension + ord()
# First N letters string construction
res = ''.join(chr(ord('a')+i) for i in range(N))
     
# print result
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 code to demonstrate
# First N letters string construction
letters = "abcdefghijklmnopqrstuvwxyz"
 
# initializing N
N = 15
 
res = letters[:N]
 
# print result
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:




# Python3 code to demonstrate
# First N letters string construction
# using bytearray
 
# initializing N
N = 15
 
# using bytearray
# First N letters string construction
res = str(bytearray(range(97, 97+N)), 'utf-8')   
# print result
print("The string after construction : " + str(res))
#this code contributed by tvsk

Output
The string after construction : abcdefghijklmno

Time Complexity: O(N).
Auxiliary Space: O(N).

Method #5:Using reduce() and join(): 

Step-by-step approach:




# import reduce function from functools module
from functools import reduce
 
# set the number of characters to construct the string
N = 15
 
# use a list comprehension to create a list of characters from 'a' to 'o'
char_list = [chr(ord('a') + i) for i in range(N)]
 
# use the reduce function to concatenate the characters in the list into a single string
res = reduce(lambda x, y: x + y, char_list)
 
# print the resulting string
print("The string after construction: " + str(res))
#This code is contributed by Jyothi pinjala

Output
The string after construction: abcdefghijklmno

Time Complexity: O(N).
Auxiliary Space: O(N).

Method #6: Using itertools.islice()

Step-by-step approach:




# Python3 code to demonstrate
# First N letters string construction using itertools.islice()
 
import itertools
 
# initializing letters
letters = "abcdefghijklmnopqrstuvwxyz"
 
# initializing N
N = 15
 
# constructing first N letters string using itertools.islice()
res = ''.join(itertools.islice(letters, N))
 
# print result
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.


Article Tags :