Tutorialspoint
Problem
Solution
Submissions

Convert a String to a List of Characters

Certification: Basic Level Accuracy: 78.72% Submissions: 141 Points: 5

Write a Python program that converts a string to a list of its individual characters.

Example 1
  • Input: "Python"
  • Output: ['P', 'y', 't', 'h', 'o', 'n']
  • Explanation:
    • Step 1: Take the input string "Python".
    • Step 2: Convert each character to an element in a list.
    • Step 3: Return the resulting list ['P', 'y', 't', 'h', 'o', 'n'].
Example 2
  • Input: "Hello, World!"
  • Output: ['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!']
  • Explanation:
    • Step 1: Take the input string "Hello, World!".
    • Step 2: Convert each character, including spaces and special characters, to an element in a list.
    • Step 3: Return the resulting list ['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!'].
Constraints
  • 0 ≤ len(string) ≤ 10^6
  • String contains printable ASCII characters
  • Time Complexity: O(n), where n is the length of the string
  • Space Complexity: O(n)
Control StructuresObject-Oriented ProgrammingFacebookPwC
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

 
  • Use list() constructor: list(string)
  • Use list comprehension: [char for char in string]
  • Use for loop: chars = []; for char in string: chars.append(char)
  • Handle empty string case: [] if not string else list(string)

The following are the steps to convert a string to a list of characters:

  • Take a string as input.
  • Use `list(string)` to convert it into a list.
  • Print the list of characters.
  • Handle empty strings correctly.

Submitted Code :