Mastering Python Strings: Types and Usage

𝐃𝐢𝐟𝐟𝐞𝐫𝐞𝐧𝐭 𝐭𝐲𝐩𝐞𝐬 𝐨𝐟 𝐬𝐭𝐫𝐢𝐧𝐠𝐬 𝐢𝐧 𝐩𝐲𝐭𝐡𝐨𝐧 Hi Amigos! I've learned about the different types of strings in Python and their usage to write clean, well-structured, and readable code. 1. 𝐒𝐢𝐧𝐠𝐥𝐞-𝐐𝐮𝐨𝐭𝐞𝐝 𝐒𝐭𝐫𝐢𝐧𝐠 Created using single quotes ' ' (s = 'Hello World'). ➡️ Used for simple text ➡️ Most commonly used 2. 𝐃𝐨𝐮𝐛𝐥𝐞-𝐐𝐮𝐨𝐭𝐞𝐝 𝐒𝐭𝐫𝐢𝐧𝐠 Created using double quotes " " (s = "Hello World"). ➡️ Same as single-quoted strings ➡️ Helpful when text contains apostrophes (s = "It's Python") 3. 𝐓𝐫𝐢𝐩𝐥𝐞-𝐐𝐮𝐨𝐭𝐞𝐝 𝐒𝐭𝐫𝐢𝐧𝐠 (𝐌𝐮𝐥𝐭𝐢-𝐥𝐢𝐧𝐞 𝐒𝐭𝐫𝐢𝐧𝐠) Created using triple single ''' ''' or triple double """ """ quotes. (s = """This is a multi-line string""") ➡️ Used for multi-line text ➡️ Often used for docstrings 4. 𝐑𝐚𝐰 𝐒𝐭𝐫𝐢𝐧𝐠 Created using r or R before the string. (s = r"C:\new\folder") ➡️ Ignores escape characters ➡️ Commonly used for file paths & regex 5. 𝐅𝐨𝐫𝐦𝐚𝐭𝐭𝐞𝐝 𝐒𝐭𝐫𝐢𝐧𝐠 (𝐟-𝐬𝐭𝐫𝐢𝐧𝐠) Created using f before the string. (name = "Sundar" age = 23 s = f"My name is {name} and I am {age} years old") ➡️ Easy variable insertion ➡️ Clean and readable 6. 𝐔𝐧𝐢𝐜𝐨𝐝𝐞 𝐒𝐭𝐫𝐢𝐧𝐠 In Python 3, all strings are Unicode by default. s = "ヘマ・スンダル" ➡️ Supports international languages ➡️ No special syntax needed 7. 𝐁𝐲𝐭𝐞 𝐒𝐭𝐫𝐢𝐧𝐠 Used to store binary data, not normal text. (s = b"Hello") ➡️ Used in networking & file handling ➡️ Data is in bytes, not characters 8. 𝐄𝐬𝐜𝐚𝐩𝐞 𝐒𝐞𝐪𝐮𝐞𝐧𝐜𝐞 𝐒𝐭𝐫𝐢𝐧𝐠 Uses special characters starting with \. (s = "Hello\nWorld") Common escape sequences: ➡️ \n → New line: - It is used to shift the sentence to new line ➡️ \t → Tab: - It is used to give Tab spaces ➡️ \\ → Backslash: - It is used to escape the backslash character so that \n and \t are treated as normal text, not as a newline or tab. #Python #LearningPython #Coding #DataAnalytics #Upskilling #TechSkills

  • text

Clear and beginner-friendly explanation of Python string types. This is really helpful for writing cleaner, more readable code—especially the raw strings and f-strings examples. Keep them coming Hema Sundar Kandra

To view or add a comment, sign in

Explore content categories