Skip to content
Related Articles

Related Articles

Python | First N letters string construction

Improve Article
Save Article
  • Last Updated : 07 Jun, 2019
Improve Article
Save Article

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 application 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 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

 

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 the 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

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!