Open In App

Python | First N letters string construction

Last Updated : 16 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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




# 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




# 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:

  • 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




# 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:

  • 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




# 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:

  • 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




# 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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads