🐍Understanding Strings in Python
Understanding Strings in Python
Strings in Python are fundamental for recording text information, such as names. They are considered a sequence, meaning Python keeps track of every element in the string. For example, Python interprets the string "Python" as a sequence of letters in a specific order. This allows us to use indexing to access particular characters, such as the first or last letter.
The concept of sequences is essential in Python, and we’ll revisit it throughout our learning journey.
In This Guide, You Will Learn:
- Creating Strings
- Printing Strings
- String Indexing and Slicing
- String Properties
- String Methods
- Print Formatting
Creating Strings
To create a string in Python, use either single or double quotes:
# Single word
print('hello')
# Line space between sentences
print()
# Entire phrase
print('This is also a string')
# Printing your name
print("Divya Rajurkar")
Variables: Store Your Value in Me!
Variables are used to store strings, making your code clean and less redundant. Assigning a value to a variable follows this syntax:
variable_name = value_or_expression
Example:
# Assigning a string to a variable
message = "Hello, Python!"
print(message)
String Indexing
Since strings are sequences, Python uses indexes to access parts of the string. Indexing starts from 0.
# Assign string to variable
s = 'Hello World'
# Print the entire string
print(s)
# Accessing specific characters
print(s[0]) # First character
print(s[1]) # Second character
print(s[2:4]) # Slice from index 2 to 3
# Accessing characters from the end
print(s[-1]) # Last character
print(s[10]) # Tenth index character
String Concatenation and Repetition
Concatenation: Combines two strings using the +
operator.
Repetition: Repeats a string multiple times using the *
operator.
# Concatenation
s1 = 'Hello'
s2 = "World"
print(s1 + " " + s2)
# Repetition
print("Hello_" * 5)
print("-" * 10)
print("darshil " * 10)
# Challenge: Print 'Data' 15 times
print("Data" * 15)
String Slicing & Indexing
Indexing selects a specific character, while slicing selects a subset of the string.
s = "Namaste World"
# Indexing
print(s[1])
# Slicing
print(s[8:12])
print(s[-5:])
# Membership Test
print("World" in s)
# More slicing
print(s[-9:-4])
Challenge:
# Print "World" and "ste" from the string
print(s[8:])
print(s[4:7])
Basic Built-in String Methods
Python strings come with built-in methods. These methods perform actions on the object:
s = "Hello World"
# Convert to uppercase
print(s.upper())
# Convert to lowercase
print(s.lower())
# Check the type of the object
print(type(s))
Print Formatting
Format strings using .format()
or f-strings:
name = "Einstein"
age = 22
married = True
# Using % formatting
print("My name is %s, my age is %s, and it is %s that I am married" % (name, age, married))
# Using .format()
print("My name is {}, my age is {}, and it is {} that I am married".format(name, age, married))
# Using f-strings
print(f"My name is {name}, my age is {age}")
Mini Challenge
Store the word "hello" in a variable and concatenate it with a name:
# Challenge
my_string = "hello"
print(my_string + name)
Keep practicing to master Python strings!
Comments
Post a Comment