Skip to content

Latest commit

 

History

History
16 lines (12 loc) · 1.21 KB

File metadata and controls

16 lines (12 loc) · 1.21 KB

Strings

1. String Data Type in Python:

  • In Python, a string is a sequence of characters, enclosed within single (' '), double (" "), or triple (''' ''' or """ """) quotes.
  • Strings are immutable, meaning you cannot change the characters within a string directly. Instead, you create new strings.
  • You can access individual characters in a string using indexing, e.g., my_string[0] will give you the first character.
  • Strings support various built-in methods, such as len(), upper(), lower(), strip(), replace(), and more, for manipulation.

2. String Manipulation and Formatting:

  • Concatenation: You can combine strings using the + operator.
  • Substrings: Use slicing to extract portions of a string, e.g., my_string[2:5] will extract characters from the 2nd to the 4th position.
  • String interpolation: Python supports various ways to format strings, including f-strings (f"...{variable}..."), %-formatting ("%s %d" % ("string", 42)), and str.format().
  • Escape sequences: Special characters like newline (\n), tab (\t), and others are represented using escape sequences.
  • String methods: Python provides many built-in methods for string manipulation, such as split(), join(), and startswith().