"Understanding Python Strings: Functions and Examples"

🧠 What is a String in Python? A string is a sequence of characters enclosed in single quotes (' '), double quotes (" "), or triple quotes (''' ''' / """ """). Example: name = "Vaibhav" Perfect 👍 Let’s go step-by-step — here are the most useful Python string functions with simple examples and outputs 👇 🧵 String Functions in Python with Examples 1️⃣ len() – Length of String text = "Python" print(len(text)) Output: 6 📘 Counts total number of characters in the string. 2️⃣ lower() – Convert to Lowercase text = "HELLO" print(text.lower()) Output: hello 3️⃣ upper() – Convert to Uppercase text = "hello" print(text.upper()) Output: HELLO 4️⃣ title() – Convert to Title Case text = "welcome to python" print(text.title()) Output: Welcome To Python 5️⃣ capitalize() – Capitalize First Letter text = "python programming" print(text.capitalize()) Output: Python programming 6️⃣ strip() – Remove Spaces text = " Python " print(text.strip()) Output: Python 7️⃣ replace(old, new) – Replace Word or Letter text = "I love Java" print(text.replace("Java", "Python")) Output: I love Python 8️⃣ find() – Find Index of Substring text = "Hello" print(text.find("l")) Output: 2 📘 Returns index of first occurrence. 9️⃣ count() – Count Occurrences text = "banana" print(text.count("a")) Output: 3 🔟 startswith() – Check Start of String text = "Python" print(text.startswith("Py")) Output: True 1️⃣1️⃣ endswith() – Check End of String text = "Python" print(text.endswith("on")) Output: True 1️⃣2️⃣ split() – Split into List text = "I love Python" print(text.split()) Output: ['I', 'love', 'Python'] 1️⃣3️⃣ join() – Join List into String words = ['I', 'love', 'Python'] print(' '.join(words)) Output: I love Python 1️⃣4️⃣ isdigit() – Check for Digits text = "12345" print(text.isdigit()) Output: True 1️⃣5️⃣ isalpha() – Check for Alphabets text = "Hello" print(text.isalpha()) Output: True 1️⃣6️⃣ isalnum() – Check for Letters and Numbers text = "abc123" print(text.isalnum()) Output: True 1️⃣7️⃣ swapcase() – Swap Upper and Lower Case text = "PyThOn" print(text.swapcase()) Output: pYtHoN #Python #PythonProgramming #PythonDeveloper #Coding #Programming #LearnPython #CodeNewbie #SoftwareDevelopment #DeveloperCommunity #TechLearning #PythonBasics #StringFunctions #CodingForBeginners #PythonTips #PythonLearning #PythonProjects #DataScience #100DaysOfCode #PythonCode #StudyPython

  • chart, diagram, bubble chart

To view or add a comment, sign in

Explore content categories