📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this blog post, we'll explore how to convert a string into a list of characters in Python.
Method 1: List Comprehension
List comprehension in Python offers a concise and readable way to create lists. It's a common Pythonic method to convert a string into a list of characters.
Example:
def string_to_list(s):
return [char for char in s]
# Testing the function
my_string = "Hello, World!"
char_list = string_to_list(my_string)
print(char_list)
Output:
['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!']
Method 2: Using the list() Function
Example:
my_string = "Hello, World!"
char_list = list(my_string)
print(char_list)
Output:
['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!']
Method 3: Using the map() Function
Example:
my_string = "Hello, World!"
char_list = list(map(lambda x: x, my_string))
print(char_list)
Output:
['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!']
Comments
Post a Comment
Leave Comment