Python Tutorial

Python Variable

Python Operators

Python Sequence

Python String

Python Flow Control

Python Functions

Python Class and Object

Python Class Members (properties and methods)

Python Exception Handling

Python Modules

Python File Operations (I/O)

Python string (including long strings and raw strings)

In Python, strings are sequences of characters enclosed in single quotes (' ') or double quotes (" "). This tutorial will guide you through working with strings in Python, including long strings and raw strings.

  1. Creating strings:

    You can create strings using either single or double quotes:

    # Creating strings
    string1 = 'Hello, World!'
    string2 = "Python is awesome!"
    
    print(string1)  # Output: Hello, World!
    print(string2)  # Output: Python is awesome!
    

    To include quotes within a string, use the opposite type of quotes or escape the quote character with a backslash (\):

    # Using quotes within strings
    string3 = 'I\'m learning Python!'
    string4 = "He said, \"Python is amazing!\""
    
    print(string3)  # Output: I'm learning Python!
    print(string4)  # Output: He said, "Python is amazing!"
    
  2. Long strings (multiline strings):

    To create multiline strings, use triple quotes (''' ''' or """ """):

    # Creating multiline strings
    multiline_string1 = '''This is a
    multiline
    string.'''
    multiline_string2 = """Another example
    of a multiline
    string."""
    
    print(multiline_string1)
    print(multiline_string2)
    
  3. Raw strings:

    Raw strings in Python are prefixed with an 'r' or 'R' before the opening quote, and they treat backslashes as literal characters instead of escape characters. This is useful when working with file paths, regular expressions, or any string containing lots of backslashes:

    # Creating raw strings
    raw_string1 = r'C:\Users\John\Desktop'
    raw_string2 = R'Regex pattern: \w+\d{4}'
    
    print(raw_string1)  # Output: C:\Users\John\Desktop
    print(raw_string2)  # Output: Regex pattern: \w+\d{4}
    
  4. String concatenation:

    You can concatenate strings using the + operator:

    first_name = "John"
    last_name = "Doe"
    full_name = first_name + " " + last_name
    
    print(full_name)  # Output: John Doe
    
  5. String formatting:

    There are several ways to format strings in Python, such as using f-strings, the str.format() method, or the % operator:

    # Using f-strings (Python 3.6+)
    name = "John"
    age = 30
    f_string = f"My name is {name} and I am {age} years old."
    print(f_string)  # Output: My name is John and I am 30 years old.
    
    # Using str.format()
    format_string = "My name is {} and I am {} years old.".format(name, age)
    print(format_string)  # Output: My name is John and I am 30 years old.
    
    # Using the % operator
    percent_string = "My name is %s and I am %d years old." % (name, age)
    print(percent_string)  # Output: My name is John and I am 30 years old.
    

In summary, Python provides various ways to work with strings, including long strings and raw strings. By understanding how to create, concatenate, and format strings, you can manipulate and display text effectively in your Python programs.

  1. Creating and initializing strings in Python:

    • Description: Strings are sequences of characters. You can create and initialize them by enclosing text in single or double quotes.
    • Code example:
      my_string = "Hello, Python!"
      
  2. String literals and constants in Python:

    • Description: String literals are directly specified in the code. Python also has constants like None for representing null or absent values.
    • Code example:
      string_literal = "This is a string"
      null_value = None
      
  3. Concatenating strings in Python:

    • Description: You can concatenate strings using the + operator or by using the join() method.
    • Code example:
      first_name = "John"
      last_name = "Doe"
      full_name = first_name + " " + last_name
      
  4. String indexing and slicing in Python:

    • Description: Strings are indexed from 0. You can access individual characters or slices of a string using indexing.
    • Code example:
      my_string = "Python"
      first_char = my_string[0]  # 'P'
      substring = my_string[1:4]  # 'yth'
      
  5. Escape characters and special sequences in Python strings:

    • Description: Escape characters (e.g., \n, \t) allow you to include special characters in strings.
    • Code example:
      special_string = "This is a line\nAnd this is a new line"
      
  6. Multiline strings in Python:

    • Description: Triple quotes (''' or """) allow you to create multiline strings.
    • Code example:
      multiline_string = """
      This is a multiline
      string in Python
      """
      
  7. Raw strings in Python:

    • Description: Raw strings (prefix r or R) treat backslashes as literal characters, useful for regular expressions or file paths.
    • Code example:
      raw_string = r"C:\Users\Username\Documents"
      
  8. Common operations and methods for strings in Python:

    • Description: Strings have various built-in methods for operations like finding, replacing, and formatting.
    • Code example:
      my_string = "Hello, Python!"
      length = len(my_string)
      uppercase = my_string.upper()