List of alphabets python – Python Program to List English Alphabets in a File by Specified Number of Letters on Each Line

List of alphabets python: Given the number of characters in each line, the task is to generate the list of English Alphabets in a file by given Number of Letters on Each Line in Python. List of alphabets in python.

Program to List English Alphabets in a File by Specified Number of Letters on Each Line in Python

Method #1: Using For loop and List Comprehension (Static Input)

Approach:

  • Give the number of characters in a line as static input and store it in a variable.
  • Take an empty string to store all the alphabets in the English language(Capital letters).
  • Here we iterate from 65(Ascii value of ‘A’) to 65+26(Z) using For loop.
  • Convert this ASCII value to the character using chr() function.
  • Add this character to the alphabets string using the string concatenation.
  • Make a single variable to store the path of the file. This is a constant value
  • This value must be replaced with the file path from your own system in the example below.
  • Open the file in write-only mode. In this case, we’re simply writing the contents of the file.
  • Use list comprehension, For loop and slicing to make numblines number of characters in each line.
  • Here we added (‘\n’) to separate the lines.
  • Write the above-modified letters to the file using the writelines function.
  • The Exit of the Program.

Below is the implementation:

# Give the number of characters in a line as static input and store it in a variable
numblines = 5
# Take a empty string to store all the alphabets in English language(Capital letters)
alphabets = ""
# Here we iterate from 65(Ascii value of 'A') to 65+26(Z) using For loop
for i in range(65, 65+26):
    # convert this ASCII value to the character using chr() function
    charvalue = chr(i)
    # add this character to alphabets string using the string concatenation
    alphabets += charvalue
# Make a single variable to store the path of the file. This is a constant value
# This value must be replaced with the file path from your own system in the example below.
filename = 'samplefile.txt'
# Open the file in write-only mode. In this case, we’re simply writingthe contents to the file.
with open(filename, "w") as file:
    # Use list comprehension,For loop and slicing to make numblines number of characters in each line
    # Here we added ('\n') to separate the lines
    modifiedletters = [alphabets[i:i + numblines] +
                       "\n" for i in range(0, len(alphabets), numblines)]
    # write the above modified letters to the file using the writelines function
    file.writelines(modifiedletters)

Output:

ABCDE
FGHIJ
KLMNO
PQRST
UVWXY
Z

Samplefile.txt:

ABCDE
FGHIJ
KLMNO
PQRST
UVWXY
Z

Method #2: Using For loop and List Comprehension (User Input)

Approach:

  • Give the number of characters in a line as user input using the int(input()) function and store it in a variable.
  • Take an empty string to store all the alphabets in the English language(Capital letters).
  • Here we iterate from 65(Ascii value of ‘A’) to 65+26(Z) using For loop.
  • Convert this ASCII value to the character using chr() function.
  • Add this character to the alphabets string using the string concatenation.
  • Make a single variable to store the path of the file. This is a constant value
  • This value must be replaced with the file path from your own system in the example below.
  • Open the file in write-only mode. In this case, we’re simply writing the contents of the file.
  • Use list comprehension, For loop and slicing to make numblines number of characters in each line.
  • Here we added (‘\n’) to separate the lines.
  • Write the above-modified letters to the file using the writelines function.
  • The Exit of the Program.

Below is the implementation:

# Give the number of characters in a line as user input using the int(input()) function and store it in a variable.
numblines = int(input('Enter the number of characters in a line you want : '))
# Take a empty string to store all the alphabets in English language(Capital letters)
alphabets = ""
# Here we iterate from 65(Ascii value of 'A') to 65+26(Z) using For loop
for i in range(65, 65+26):
    # convert this ASCII value to the character using chr() function
    charvalue = chr(i)
    # add this character to alphabets string using the string concatenation
    alphabets += charvalue
# Make a single variable to store the path of the file. This is a constant value
# This value must be replaced with the file path from your own system in the example below.
filename = 'samplefile.txt'
# Open the file in write-only mode. In this case, we’re simply writingthe contents to the file.
with open(filename, "w") as file:
    # Use list comprehension,For loop and slicing to make numblines number of characters in each line
    # Here we added ('\n') to separate the lines
    modifiedletters = [alphabets[i:i + numblines] +
                       "\n" for i in range(0, len(alphabets), numblines)]
    # write the above modified letters to the file using the writelines function
    file.writelines(modifiedletters)

Output:

Enter the number of characters in a line you want : 7

Samplefile.txt:

ABCDEFG
HIJKLMN
OPQRSTU
VWXYZ

Sample Implementation in google colab:

Answer these:

  1. How To Create A List Of Alphabets In Python
  2. Alphabet Python List
  3. Python Code For Alphabets
  4. English Alphabet List Python
  5. Python Generate Alphabet List
  6. List Of Alphabet In Python
  7. Python Iterate Alphabet
  8. Python List Of All Alphabets