Automatically translated.View original post

🧠 Daily Python Problem: พลิกคำ (Reverse String) 🔄

Today's problem is to write the reverse _ string function to restore the original string that was reversed from back to front!

🎯 Problem:

Write a function that receives String and restores String at Reverse. All letters, such as "hello," become "olleh."

💡 2 must-know solutions in Python!

1. Basic Type: Use Loop 🧱

• Create an empty String variable

• Loop Loop Read one letter at a time from the original String

• Per new letter in front of String, always blank (reversed _ text = char + reversed _ text)

• This method helps us understand the principle of working step by step.

2.Pythonic: Use Slicing (Pro Technique!) ✨

• No Loop required!

• Use the powerful Slicing technique: text [:: -1]

• [:: -1] means: start (start) anywhere (empty), finish (stop) anywhere (empty), but step (step) backwards in 1 (-1) increments.

• This method is short, concise and popular among Python Developers.

Which method do you like better? Try to run both codes.

# python # data # ai # data aanalyst # learnonlemon8

2025/10/30 Edited to

... Read moreถ้าเพิ่งเริ่มทำโจทย์ Reverse String สิ่งที่หลายคนสะดุดคือ “ตัวป้อน (input) คืออะไร” และควรออกแบบฟังก์ชันให้รับค่าแบบไหนกันแน่ จากที่ลองสอน/ลองทำโจทย์แนวนี้บ่อย ๆ ฉันแนะนำให้คิดเป็น 2 แบบตามบริบท: (1) รับ input เป็นพารามิเตอร์ของฟังก์ชัน เช่น reverse_string(text) เหมาะกับการนำไปใช้ต่อ/เขียนเทสต์ (2) รับ input จากผู้ใช้ผ่าน input() เหมาะกับการทำโปรแกรมเล็ก ๆ ในคอนโซล ตัวอย่างแบบ “รับ input จากผู้ใช้” ที่ชัดเจน: - รับค่า: text = input("กรอกข้อความ: ") - ประมวลผล: reversed_text = text[::-1] - แสดงผล: print(reversed_text) ข้อดีคือเห็นภาพทันทีว่าตัวป้อน (input) ไหลเข้าระบบยังไง แต่ถ้าจะให้โค้ดดูเป็นมาตรฐานมากขึ้น แนะนำให้แยก logic ออกเป็นฟังก์ชัน แล้วค่อยเรียกจากส่วน input/output เช่น: - def reverse_string(text): return text[::-1] - text = input("กรอกข้อความ: ") - print(reverse_string(text)) อีกเรื่องที่คนเจอบ่อยคือ input ไม่ได้มีแค่ตัวอักษร เช่น “12345” หรือมีเว้นวรรค “Hello World” ซึ่งจริง ๆ แล้วใน Python ค่า input() จะได้เป็น “สตริง” เสมอ ดังนั้นเคสตัวเลขก็จะถูกมองเป็นตัวอักษร ‘1’, ‘2’, ‘3’… ทำให้พลิกแล้วได้ “54321” ตามคาด ส่วนข้อความที่มีเว้นวรรคก็พลิกได้ทั้งสตริงเหมือนกัน เช่น “ab cd” จะกลายเป็น “dc ba” ทิปเล็ก ๆ ที่ช่วยให้โค้ดใช้งานจริงได้ดีขึ้น: 1) จัดการช่องว่างหัวท้าย: ถ้าไม่อยากให้ enter/space เกิน ๆ มีผล ใช้ text = input(...).strip() 2) รองรับสตริงว่าง: ถ้าผู้ใช้กด enter เฉย ๆ ฟังก์ชันควรคืนค่า "" ได้ ไม่ควร error 3) ถ้าอยาก reverse “คำ” ไม่ใช่ “ตัวอักษร”: คนละโจทย์เลย ต้องใช้ split() แล้ว reverse list เช่น "I love Python" -> "Python love I" สุดท้าย ถ้าโจทย์ระบุชัดว่าให้ “รับ String และคืนค่า String ใหม่ที่ Reverse” วิธี slicing `[::-1]` คือทางลัดที่อ่านง่ายและเร็วมาก ส่วนแบบ loop เหมาะกับการฝึกตรรกะ (และช่วยให้เข้าใจเรื่องการต่อสตริง/การวนซ้ำ) ลองเริ่มจากแบบพื้นฐานก่อน แล้วค่อยสรุปด้วยวิธี Pythonic จะจำได้แม่นขึ้นค่ะ

Related posts

Learn the basics of computer science & coding ✨
This book caught my eye and I don't regret purchasing it. This book is filled with bite-sized info to learn computer science and coding, and the information was also very well presented. When I shared this book one day, I got a lot of messages about the book details so I also included them here
teal.days

teal.days

1734 likes

how I got started learning Python 🐍💻👾
I’ve been learning Python this summer to prepare for my research lab and courses in the fall. Programming isn’t required for my major, so I’m taking the time to learn it alone. The biggest tip I can give is specific goals from the beginning! Speaking from experience, this will save a lot of time an
reagan

reagan

46 likes

Why analysts should use Python for Data Processing
Hello Data Analysts! I wanted to highlight Python's efficiency in data cleaning and preprocessing. Python libraries like Pandas and NumPy offer methods to handle missing data, transform data types, and encode categorical variables like one-hot encoding. Tools such as drop_duplicates() ensure da
Yun Jung

Yun Jung

43 likes

NOTION TOUR ✨how I stay organized w/ notion!
welcome to my notion! I use notion for literally EVERYTHING. it’s a fantastic tool to stay organized with. while notion can feel a little overwhelming at first, templates are a great way to get started. here’s a lil overview of my page: • directory page - I use this page to quickly acces
adrianna

adrianna

60 likes

✨ String Methods You’ll Use a Lot
String Methods You’ll Use a Lot Python strings are everywhere — names, messages, input, output. Master these basics and everything gets easier 🚀 #Python #PythonStrings #LearnPython #CodingBasics #Programming
TechKeysX

TechKeysX

0 likes

What is String Slicing? 🔪
What is String Slicing? String slicing lets you extract parts of a string using indexes. It’s fast, simple, and super useful. #Python #LearnPython #PythonBasics #PythonStrings
TechKeysX

TechKeysX

0 likes

An iPad in a pink composition book-style case with a pink stylus, white headphones, and a white tube on a wooden surface. The image has text overlayed: '5 WAYS TO USE YOUR IPAD'.
An iPad displaying a digital journal entry titled 'MISSING THE HOMELAND' with various images, text blocks, and a music player widget, set against a dark background with calendar months at the top.
An iPad displaying a font creation interface. The screen shows an alphabet, a large 'A' being designed with grid lines, and tools at the bottom, under the title 'CREATE YOUR OWN FONT'.
5 (more) ways to use your iPad 👩🏻‍💻✍🏻💕
You guys seemed to like the last post, so here are some more ways to use your iPad! ☺️✍🏻💕 1. Digital journaling: turn your iPad into an art journal, junk journal, diary, scrapbook… it is so versatile!! Not only are you saving paper, but you’re also getting to unwind and be creative ✍🏻💕 2. Cre
enchantia

enchantia

27.8K likes

The cover image features an illustration of a person using a laptop with a code editor window, introducing the topic: 'Top 5 Beginner-Friendly Programming Languages for Career Switchers.'
This image presents an illustration of people interacting with a large monitor displaying code symbols, alongside text encouraging career switchers to choose a beginner-friendly programming language for tech.
This slide details HTML & CSS, showing a structural diagram of a webpage and its styled result. It explains why to learn them, highlighting their role in web design and suitability for creative minds.
Top 5 Programming Languages for Career Switchers
#codingforbeginners #learntocode #careerswitch #python #htmlcssjavascript
Aysha

Aysha

29 likes

A tablet displays the British Council IELTS test selection page, highlighting benefits of IELTS on Computer like fast results, One Skill Retake, over 40 practice sets, and convenient test centers near BTS/MRT.
This image details the advantages of IELTS on Computer with British Council, including noise-reducing headphones for Listening, highlighting and integrated questions for Reading, automatic word count for Writing, and a real examiner for Speaking, with results in 2 days.
The image showcases 'IELTS Ready Premium' features, offering over 40 practice sets for all skills, online lessons, and practice tests. It also shows an example writing task and links to free online practice tests on the British Council website.
สอบ IELTS on Computer กับ British Council ดียังไง?
สวัสดีค่า สำหรับใครที่กำลังวางแผนสอบ IELTS ไม่ว่าจะเพื่อยื่นคะแนนเข้ามหาลัยอินเตอร์ เรียนต่อ ทำงาน หรือยื่นวีซ่า วันนี้เคทมีอีกหนึ่งทางเลือกในการสอบ IELTS มาแนะนำ นั่นก็คือ IELTS on Computer ของ British Council ค่ะ เป็นรูปแบบการสอบที่ช่วยให้การสอบ “จัดการง่ายขึ้น” และเตรียมตัวได้มั่นใจขึ้นจริง ๆ หล
✿Kareburuʾʾ

✿Kareburuʾʾ

7 likes

Don’t Let AI Make You a Lazy Coder
#codewithaysha #aiforcoders #promptengineering #codingtips #chatgptfordev
Aysha

Aysha

21 likes

Hi yall
I lost my phone again. Its ok tho we got it back! #summerbod #bodytransformation #unfiltered
kbarrett2

kbarrett2

2 likes

An olive green title card features white text 'Let's Talk Deities' above a classical bust illustration of Apollo. Below the bust, it reads 'APOLLO GREEK GOD OF THE SUN, POETRY, MUSIC, AND HEALING'. The 'lemon8' logo and '@thebakedwitchcraft' are at the bottom.
Let’s Talk Deities: Apollo Apollo is the God of music, poetry, arts, oracles, prophecy, truth, knowledge, archery, herds/flocks, diseases (specifically the plague), healing, medicine, light, sun, knowledge, and protection of young. The Twins Zeus & Leto with Apollo and Artemis.
The Baked Witch

The Baked Witch

91 likes

“ใกล้ 40 แล้ว! ทำไมเส้นพลิก ปวดตัวง่าย? แก้ยังไงดี! 💪” ep1
#CapCut
จัสมิน กินได้ ไม่อ้วน

จัสมิน กินได้ ไม่อ้วน

1 like

การออกกําลังกายและการออกกําลังกายเป็นสิ่งที่ขาดไม่ได้
Sidagar #sidagar #lemon8bookclub #Lemon8Diary #cup #tumbler
Sidagar TH6

Sidagar TH6

0 likes

Seeds
getting ready for #spring
that is my name

that is my name

0 likes

Dachshund problems
Sorry for interrupting, but mommy really needs to fix that ear. 🤣 ขอโทษที่ขัดจังหวะนะลูก แต่เห็นหูพลิกแล้ว อดใจไม่ไหวจริงๆ ~ Tanggwa (Cucumber 🥒) ~ 💛 . . #dogoflemon8 #dachshund #doxie #dachshundproblems #Dackel
Tanggwa the Sausage Dog

Tanggwa the Sausage Dog

3 likes

A Muji 2024 vertical planner in B6 size is displayed open on a desk with pens and other stationery, showing a weekly spread with handwritten entries. The image highlights the user's setup for their Muji planner.
A Muji 2024 vertical planner in B6 size with a clear cover is shown on a desk next to a gray pen and a green highlighter. The planner's label displays product details and pricing.
A close-up of the Muji 2024 vertical planner in B6 size, highlighting its clear cover with blind embossed '2024' and a label detailing its features, including 'Monday start Vertical planner' and various pricing information.
Muji 2024 Planner ✨📅 how I use my planner
📅 item: Muji 2024 vertical planner in B6 ⭐ I got my new Muji 2024 vertical planner recently and I started on this planner about 2 weeks ago. so far I really like the layout and it’s very useful for my needs as a student! In the past I’ve gotten the 2020 weekly in A6 and 2022 weekly in A5 and I f
Emily H.

Emily H.

1738 likes

A black Longchamp Le Pliage tote bag, partially unzipped, sits on a dark desk in an office setting. An AirPods case is visible in front of the bag, highlighting its use as a work bag.
An open black Longchamp Le Pliage tote bag on a blue seat, revealing a laptop and charging cables inside. This demonstrates the bag's spacious interior for carrying work essentials and more.
A close-up view of the black Longchamp Le Pliage tote bag, showing its material composition. The image highlights the genuine leather trim and the coated canvas body of the bag.
Longchamp Le Pliage Tote Bag Review 🖤
I got my first job after my master’s program in January and to celebrate, I got this Longchamp Le Pliage bag (which was a major upgrade from my university backpack) 💫 I’ve used it now nearly every day for around 8 months, so here are my thoughts: ✨Details Model: Longchamp Le Pliage Green Lar
yasminmoradi

yasminmoradi

108 likes

Typecase & Light Pink Keyboard Case🫶
For 12.9” & 13” iPads 💻 New library aesthetic unlocked. Light pink Typecase + iPad = study in style. ✨ #typecase #ipadkeyboard #ipad13inch #pinkgirl #studentessentials
typecase_official

typecase_official

11 likes

A pink retro-style mechanical keyboard with round keys and a white Apple Magic Keyboard with a white mouse are displayed on a desk with a cute mousepad and decorative figurines.
A comparison chart detailing features like typing feel, durability, customization, and noise level between mechanical keyboards and Apple Magic Keyboards, with small images of both keyboard types above.
A comparison chart titled 'Portability and Usage,' outlining differences in portability, best use cases, gaming suitability, and design between mechanical keyboards and Magic Keyboards.
✿˖° Mechanical Keyboard vs. Apple’s Magic Keyboard
°𐐪♡𐑂°‧₊ ✨Trying to choose the right keyboard? Mechanical keyboards are tactile, clicky, and satisfying to type on, making them perfect for gaming, coding, or long typing sessions. They’re durable, customizable, and bold, but heavier and louder. Apple’s Magic Keyboards, on the other hand, are sleek
peachiesuga ♡

peachiesuga ♡

33 likes

“ใกล้ 40 แล้ว! ทำไมเส้นพลิก ปวดตัวง่าย? แก้ยังไงดี! 💪” ep2
#CapCut
จัสมิน กินได้ ไม่อ้วน

จัสมิน กินได้ ไม่อ้วน

2 likes

A woman in a pink sweater and white pants stands outside a building, with text overlaying "Experience of a Nursing Assistant (CNA) in America."
A collage showing a masked person at an Assisted Living facility, a quiet nursing station, affordable staff meals, and a payslip detailing bi-weekly earnings and good benefits.
A collage showing a TV screen with an actor, representing the elderly person's favorite show, and an elderly person eating, illustrating one-on-one care in an apartment setting.
รีวิวอาชีพ CNA พนักงานผู้ช่วยพยาบาลในอเมริกา 🇺🇲
🩵Assisted living จบใหม่เราไปสมัครงาน Assisted living คล้ายบ้านพักคนชรา สำหรับคนชราที่ดูแลตัวเองได้ และไม่ได้แต่ไม่ป่วยหนัก รายได้โอเค ชม.ละ $14/ชม. (ตอนนั้นรายได้ขั้นต่ำ $10/ชม.) แอบเหนื่อยหน่อย ต้องดูแลตั้ง 10 คน ดีแต่มีสวัสดิการ และโบนัสแรกเข้าถึง $1,500 😆 เงินออกทุกสองสัปดาห์ สามารถปล่อยเวรหรือ
Jenny

Jenny

66 likes

iPad combo touch keyboard by Logitech
TAKE iPAD FURTHER WORK, LEARN & CREATE FROM ANYWHERE WITH COMBO TOUCH Meet the keyboard case that opens up whole new levels of versatility. Type, view, sketch, and read with iPad Pro, iPad Air or iPad — all while keeping the front, back, and corners protected. With a Multi-Touch trackpad an
Sasa Koh

Sasa Koh

3 likes

From Panic to Peace: How CamScanner Saved My Exams
I have to admit that the universe never gives me a second of peace 😨 I still remember that day — final exam for Python 1. Only 2 hours left and just one cheatsheet remaining. And then… I spilled matcha all over it 😔 I was panicking and completely stressed out. Thankfully, with the recovery fea
studywithemmane

studywithemmane

69 likes

What code are you learning?👩🏼‍💻
Currently losing my mind over a different line using Javascript 😫 I've had some experience with RStudio, but I'm yet to try out Python or any other programs. Have you worked with any of these or more? #womeninstem #coding #codinggirl #gradstudent #collegelife
Jossalyn <3

Jossalyn <3

10 likes

See more