0% found this document useful (0 votes)
5 views

Data Structures and Strings in Python Dark Mode

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Data Structures and Strings in Python Dark Mode

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Data Structures and Strings in

Python
A Comprehensive Overview

Presenter: Vamsi Pothabathuni


What Are Data Structures?
• Definition:
• - Data structures are ways of organizing and storing data so that they can
be accessed and modified efficiently.

• Importance:
• - Efficient data management
• - Optimal algorithm performance
Why Data Structures?
• Benefits:
• - Improved efficiency and performance
• - Better data organization and manipulation
• - Crucial for problem-solving in programming and real-world applications
Types of Data Structures
• Overview:
• - Linear: Arrays, Linked Lists, Stacks, Queues
• - Non-linear: Trees, Graphs
• - Hash-based: Hash Tables, Dictionaries
• - Others: Strings, Sets
Introduction to Strings
• What is a String?
• - A string is a sequence of characters.

• Defining a String in Python:


• ```python
• my_string = 'Hello, World!'
• ```
How to Work with Strings in
Python
• Basic Operations:
• - Creating, accessing, and modifying strings

• Example:
• ```python
• my_string = 'Python'
• print(my_string[0]) # Output: 'P'
• ```
String Functions in Python
• Common Functions:
• - `len()`: Returns the length of the string.
• - `max()`: Returns the max alphabetical character from the string.
• - `min()`: Returns the min alphabetical character from the string.

• Example:
• ```python
• my_string = 'Python'
• print(len(my_string)) # Output: 6
• print(max(my_string)) # Output: 'y'
• print(min(my_string)) # Output: 'P'
• ```
Getting All Properties of String
Type
• Using `dir()` and `help()`:
• - `dir()`: Lists all properties and methods of a string.
• - `help()`: Provides detailed information about methods.

• Example:
• ```python
• print(dir(str))
• help(str)
• ```
String Indexing and Slicing
• Indexing:
• ```python
• my_string = 'Python'
• print(my_string[0]) # Output: 'P'
• ```

• Slicing:
• ```python
• print(my_string[0:2]) # Output: 'Py'
• ```
String Concatenation and
Multiplication
• Concatenation:
• - Combines two or more strings.
• ```python
• str1 = 'Hello'
• str2 = 'World'
• print(str1 + ' ' + str2) # Output: 'Hello World'
• ```

• Multiplication:
• - Repeats a string multiple times.
• ```python
• print(str1 * 3) # Output: 'HelloHelloHello'
• ```
String Split and Max Split
• Split:
• - Splits a string into a list based on a delimiter.
• ```python
• my_string = 'Python is fun'
• print(my_string.split()) # Output: ['Python', 'is', 'fun']
• ```

• Max Split:
• - Splits a string into a list with a maximum number of splits.
• ```python
• print(my_string.split(' ', 1)) # Output: ['Python', 'is fun']
• ```
String Capitalize and Title
• Capitalize:
• - Converts the first character to uppercase.
• ```python
• my_string = 'python is fun'
• print(my_string.capitalize()) # Output: 'Python is fun'
• ```

• Title:
• - Converts the first character of each word to uppercase.
• ```python
• print(my_string.title()) # Output: 'Python Is Fun'
• ```
String Count and Replace
• Count:
• - Returns the number of occurrences of a substring.
• ```python
• my_string = 'Python is fun'
• print(my_string.count('o')) # Output: 1
• ```

• Replace:
• - Replaces a substring with another substring.
• ```python
• print(my_string.replace('fun', 'awesome')) # Output: 'Python is awesome'
• ```
String Upper, Lower, and Swapcase
• Upper:
• - Converts all characters to uppercase.
• ```python
• print(my_string.upper()) # Output: 'PYTHON IS FUN'
• ```

• Lower:
• - Converts all characters to lowercase.
• ```python
• print(my_string.lower()) # Output: 'python is fun'
• ```

• Swapcase:
• - Converts uppercase characters to lowercase and vice versa.
String Reverse and Sort
• Reverse:
• - Reverses the string.
• ```python
• print(my_string[::-1]) # Output: 'nuf si nohtyP'
• ```

• Sort:
• - Sorts the characters of the string.
• ```python
• sorted_string = ''.join(sorted(my_string))
• print(sorted_string) # Output: ' Pfinnotuy'
• ```
String Join and Strip
• Join:
• - Joins elements of a list into a string.
• ```python
• words = ['Python', 'is', 'fun']
• print(' '.join(words)) # Output: 'Python is fun'
• ```

• Strip:
• - Removes leading and trailing spaces.
• ```python
• print(' Hello '.strip()) # Output: 'Hello'
• print(' Hello '.lstrip()) # Output: 'Hello '
• print(' Hello '.rstrip()) # Output: ' Hello'
• ```
String Length, Max, and Min
• Length:
• - Returns the length of the string.
• ```python
• print(len(my_string)) # Output: 13
• ```

• Max and Min:


• - Returns the max/min alphabetical character from the string.
• ```python
• print(max(my_string)) # Output: 'y'
• print(min(my_string)) # Output: ' '
• ```
String Partition
• Partition:
• - Splits the string into three parts based on a separator.
• ```python
• print(my_string.partition('is')) # Output: ('Python ', 'is', ' fun')
• ```
String Starts With and Ends With
• Starts With:
• - Checks if the string starts with a specified substring.
• ```python
• print(my_string.startswith('Py')) # Output: True
• ```

• Ends With:
• - Checks if the string ends with a specified substring.
• ```python
• print(my_string.endswith('fun')) # Output: True
• ```
String Isdigit, Isalpha, and Isalnum
• Isdigit:
• - Checks if all characters in the string are digits.
• ```python
• print(my_string.isdigit()) # Output: False
• ```

• Isalpha:
• - Checks if all characters in the string are alphabets.
• ```python
• print(my_string.isalpha()) # Output: False
• ```
String Isalnum

• Isalnum:
• - Checks if all characters in the string are alphanumeric.

• ```python
• alphanumeric_string = 'Python3'
• print(alphanumeric_string.isalnum())
• # Output: True
• ```
Conclusion
• Summary:
• - Data structures are crucial for efficient data management.
• - Strings are versatile and offer numerous methods for manipulation.

• Q&A:
• - Invite questions from the audience.

You might also like