Python Tutorial

Python Flow Control

Python Functions

Python Data Types

Python Date and Time

Python Files

Python String

Python List

Python Dictionary

Python Variable

Python Input/Output

Python Exceptions

Python Advanced

Python Strings

Python Strings Tutorial

Strings are a sequence of characters and are one of the most commonly used data types in Python. In this tutorial, we'll cover creating strings, accessing characters, basic string operations, and common string methods.

  • Creating strings

You can create a string by enclosing characters in single quotes (') or double quotes (").

string1 = 'Hello, World!'
string2 = "Hello, World!"

For multiline strings, use triple quotes (''' or """).

multiline_string = '''Hello, World!
This is a multiline
string.'''
  • Accessing characters

You can access individual characters in a string using indexing and slicing.

  • Indexing: Use square brackets [] with the index of the character you want to access.
text = "Hello, World!"
first_char = text[0]
print(first_char)  # Output: H
  • Slicing: Use square brackets [] with a start and end index separated by a colon : to access a substring.
text = "Hello, World!"
substring = text[0:5]
print(substring)  # Output: Hello
  • Basic string operations
  • Concatenation: Use the + operator to join two strings.
string1 = "Hello"
string2 = "World"
result = string1 + ", " + string2 + "!"
print(result)  # Output: Hello, World!
  • Repetition: Use the * operator to repeat a string a specified number of times.
text = "Hello! "
result = text * 3
print(result)  # Output: Hello! Hello! Hello!
  • Length: Use the len() function to find the length of a string.
text = "Hello, World!"
length = len(text)
print(length)  # Output: 13
  • Common string methods
  • lower(): Converts a string to lowercase.
text = "Hello, World!"
lowercase_text = text.lower()
print(lowercase_text)  # Output: hello, world!
  • upper(): Converts a string to uppercase.
text = "Hello, World!"
uppercase_text = text.upper()
print(uppercase_text)  # Output: HELLO, WORLD!
  • replace(old, new): Replaces all occurrences of the old substring with the new substring.
text = "Hello, World!"
replaced_text = text.replace("World", "Python")
print(replaced_text)  # Output: Hello, Python!
  • strip(): Removes leading and trailing whitespace characters (spaces, tabs, and newlines) from a string.
text = "   Hello, World!   "
stripped_text = text.strip()
print(stripped_text)  # Output: Hello, World!
  • split(separator): Splits a string into a list of substrings based on the specified separator.
text = "Hello, World!"
split_text = text.split(", ")
print(split_text)  # Output: ['Hello', 'World!']
  • join(iterable): Joins elements of an iterable (e.g., list or tuple) into a single string using the string as a separator.
words = ["Hello", "World!"]
text = ", ".join(words)
print(text)  # Output: Hello, World!
  1. Creating and manipulating strings in Python:

    • Description: Strings in Python are created using single or double quotes. They can be manipulated using various operations and methods.
    • Example Code:
      string1 = "Hello"
      string2 = 'World'
      concatenated_string = string1 + " " + string2
      
  2. String concatenation in Python:

    • Description: Concatenate (combine) multiple strings into a single string.
    • Example Code:
      string1 = "Hello"
      string2 = 'World'
      concatenated_string = string1 + " " + string2
      
  3. String indexing and slicing in Python:

    • Description: Access individual characters or substrings using index and slice notation.
    • Example Code:
      original_string = "Python"
      first_char = original_string[0]
      substring = original_string[1:4]
      
  4. Python string methods and functions:

    • Description: Python provides a variety of built-in string methods and functions for common string operations.
    • Example Code:
      original_string = "Python is amazing"
      length = len(original_string)
      uppercase_string = original_string.upper()
      
  5. String formatting in Python:

    • Description: Format strings using placeholders, the .format() method, or f-strings for concise and readable output.
    • Example Code:
      name = "Alice"
      age = 30
      formatted_string = f"My name is {name} and I am {age} years old."
      
  6. Escape sequences in Python strings:

    • Description: Use escape sequences (\n, \t, etc.) to represent special characters within strings.
    • Example Code:
      special_string = "Tabbed \t text and new line \n Next line"
      
  7. Checking string length in Python:

    • Description: Determine the length of a string using the len() function.
    • Example Code:
      original_string = "Python"
      length = len(original_string)
      
  8. String comparison and equality in Python:

    • Description: Compare strings using operators like ==, !=, <, >, etc.
    • Example Code:
      string1 = "Hello"
      string2 = 'Hello'
      is_equal = (string1 == string2)
      
  9. Common string operations and tasks in Python:

    • Description: Perform various tasks such as finding substrings, replacing characters, and converting case.
    • Example Code:
      original_string = "Python is versatile"
      substring_exists = "is" in original_string
      replaced_string = original_string.replace("versatile", "powerful")