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)) |
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)) |
The string after construction : abcdefghijklmno
Recommended Posts:
- Python | Ways to sort letters of string alphabetically
- Python | Insert value after each k letters in given list of string
- Python program to verify that a string only contains letters, numbers, underscores and dashes
- Python regex to find sequences of one upper case letter followed by lower case letters
- Python | Remove all characters except letters and numbers
- Python - Dictionary construction from front-rear key values
- Find the first repeated word in a string in Python using Dictionary
- Python | First Non-Empty String in list
- Python | Lowercase first character of String
- Print first n distinct permutations of string using itertools in Python
- How to capitalize first character of string in Python
- Python program to capitalize the first and last character of each word in a string
- Python - Group list by first character of string
- Python | First character occurrence from rear String
- Python - Separate first word from String
- Python - First K consecutive digits in String
- String slicing in Python to rotate a string
- Python | Check if a given string is binary string or not
- Python | Check if given string can be formed by concatenating string elements of list
- Python | Check if string ends with any string in given list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

