StudyLover
  • Home
  • Study Zone
  • Profiles
  • Contact us
  • Sign in
StudyLover Strings Slicing
Download
  1. Python
  2. Text Processing with Python
Assigning Strings : String Methods
Text Processing with Python

Strings as Arrays

While Python doesn't have a dedicated array data type like some other languages, strings can be treated as sequences of characters, which effectively makes them similar to character arrays.

Accessing Individual Characters

You can access individual characters in a string using indexing, just like accessing elements in a list:

Python

my_string = "Hello"

first_char = my_string[0]  # Output: 'H'

Slicing Strings

You can extract a substring using slicing:

Python

my_string = "Python"

substring = my_string[2:5]  # Output: 'tho'

String Length

To find the length of a string, use the len() function:

Python

my_string = "Hello"

length = len(my_string)  # Output: 5

Iterating Over Characters

You can iterate over the characters in a string using a for loop:

Python

for char in "Python":

  print(char)

Important Considerations

·         Immutability: Strings are immutable, meaning you cannot change individual characters.

·         Indexing starts from 0: The first character has an index of 0.

·         Negative indices can be used to access characters from the end of the string.


String Length

To find the length of a string in Python, you use the built-in len() function. It counts the number of characters in the string, including spaces, punctuation, and special characters.

Python

my_string = "Hello, world!"

length = len(my_string)

print(length)  # Output: 13

Explanation:

·         The len() function takes a string as input.

·         It returns an integer representing the number of characters in the string.

·         In the example above, the length of the string "Hello, world!" is 13.

 

in and not in Operators with Strings

Python provides the in and not in operators to check for the presence or absence of a substring within a string.

The in operator

The in operator returns True if a specified substring is found within the string, otherwise it returns False.

Python

text = "Hello, world!"

substring = "world"

 
if substring in text:

  print("Substring found")

else:

  print("Substring not found")

The not in operator

The not in operator is the opposite of in. It returns True if a specified substring is not found within the string, otherwise it returns False.

Python

text = "Hello, world!"

substring = "python"

 
if substring not in text:

  print("Substring not found")

else:

  print("Substring found")

Key points:

·         The in and not in operators are case-sensitive.

·         These operators can also be used with other sequence types like lists, tuples, and sets.

·         You can use these operators within conditional statements to control program flow.

Example:

Python

vowels = "aeiouAEIOU"

word = "hello"

 
for char in word:

  if char in vowels:

    print(char, "is a vowel")

By using the in and not in operators, you can efficiently check for the presence or absence of substrings within strings, making your Python code more concise and readable.


String Slicing

String slicing is a powerful technique to extract substrings from a given string. It involves specifying a range of indices within square brackets.

Basic Syntax:

Python

string[start:end:step]

·         start: Optional starting index (inclusive). Default is 0.

·         end: Optional ending index (exclusive). Default is the end of the string.

·         step: Optional step size. Default is 1.

Examples:

Python

text = "Hello, World!"

 
# Extract a substring from index 0 to 4 (exclusive)

substring1 = text[0:5]  # Output: "Hello"

 
# Extract from index 7 to the end

substring2 = text[7:]  # Output: "World!"

 
# Extract every second character

substring3 = text[::2]  # Output: "Hlo ol!"

 
# Reverse the string

substring4 = text[::-1]  # Output: "!dlroW ,olleH"

Detailed Explanation:

·         Positive indices: Start from the beginning of the string, with 0 being the first character.

·         Negative indices: Start from the end of the string, with -1 being the last character.

·         Omitting start or end: If you omit the start index, it defaults to 0. If you omit the end index, it defaults to the end of the string.

·         Step value: The step value determines the increment between indices. A negative step value reverses the string.

Common Use Cases:

·         Extracting substrings from a larger string.

·         Reversing strings.

·         Creating new strings by combining parts of existing strings.

By mastering string slicing, you can efficiently manipulate and extract information from strings in your Python programs.

Assigning Strings String Methods
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website