Python Basics 2
String Concatenation, Indexing, Slicing...
Index
String Concatenation
Indexing
Slicing
Advanced Slicing
String Functions
Conditional Statements
Indentation
1. String Concatenation
Definition:
String concatenation in Python refers to the operation of joining two or more strings end-to-end to form a single string.
Syntax:
pythonstring1 + string2 + ... + stringN
Examples:
Basic Example:
greeting = "Hello" + " " + "World"
print(greeting)
Output:Hello WorldAdvanced Example:
name = "Vishal"
full_greeting = "Hello, " + name + "!" print(full_greeting)
Output:Hello, Vishal!Rules:
Only strings can be concatenated using the
+operator. Attempting to concatenate other data types will raise aTypeErrorunless they are explicitly converted to strings.
2. Indexing
Definition:
Indexing refers to accessing individual characters in a string using their position. Python uses zero-based indexing, where the first character has an index of 0.
Syntax:
string_variable[index]Examples:
Basic Example:
word = "Python"
print(word[0])
Output:PAdvanced Example:
word = "Python"
print(word[-1])
Output:nRules:
Positive indices count from the start, and negative indices count from the end of the string.
3. Slicing
Definition:
Slicing allows you to access a substring by specifying a start and end index. The slice includes characters from the start index up to, but not including, the end index.
Syntax:
string_variable[start:end]Examples:
Basic Example:
word = "Python"
print(word[1:4])
Output:ythAdvanced Example:
word = "Python"
print(word[:4])
Output:PythRules:
Omitting the start index defaults to 0, and omitting the end index slices to the end of the string.
4. Advanced Slicing
Definition:
Advanced slicing allows you to extract elements from a string using a step, which specifies how many characters to skip.
Syntax:
string_variable[start:end:step]Examples:
Basic Example:
word = "Python"
print(word[::2])
Output:PtoAdvanced Example:
word = "Python"
print(word[::-1])
Output:nohtyPRules:
A negative step value reverses the direction of the slicing.
5. String Functions
Definition:
String functions are built-in methods in Python that allow various operations such as transforming, searching, and formatting strings.
Common String Functions:
lower(): Converts all characters to lowercase.
upper(): Converts all characters to uppercase.
strip(): Removes leading and trailing whitespace.
find(): Searches for a substring and returns its first occurrence index.
replace(): Replaces occurrences of a substring with another substring.
Syntax:
string_variable.method(arguments)Examples:
Basic Example:
text = "Hello World"
print(text.lower())
Output:hello worldAdvanced Example:
text = "Hello World"
new_text = text.replace("World", "Python")
print(new_text)
Output:Hello PythonRules:
String methods do not modify the original string; they return a new string.
6. Conditional Statements
Definition:
Conditional statements allow decision-making in Python by executing code blocks based on whether a condition is true or false.
Syntax:
if condition: # code block
elif another_condition: # another code block
else: # final code blockExamples:
Basic Example:
x = 10 if x > 5:
print("x is greater than 5")
Output:x is greater than 5Advanced Example:
x = 10 if x > 15:
print("x is greater than 15")
elif x > 5:
print("x is greater than 5 but less than or equal to 15")
else: print("x is 5 or less")
Output:x is greater than 5 but less than or equal to 15Rules:
Conditional statements require proper indentation to define code blocks.
7. Indentation
Definition:
Indentation in Python is crucial as it defines the structure and hierarchy of the code. Unlike other programming languages that use braces or keywords, Python uses indentation to group statements.
Syntax:
if condition: # indented code block statementExamples:
Basic Example:
x = 5
if x > 0:
print("x is positive")
Output:x is positiveAdvanced Example:
x = 5 if x > 0:
print("x is positive")
if x > 3:
print("x is greater than 3")
Output:x is positivex is greater than 3Rules:
Consistent use of spaces or tabs is essential. Mixing them can lead to errors.
This issue of FM University introduces essential Python string operations and flow control mechanisms, providing a solid foundation for your coding journey. Continue exploring these concepts to enhance your Python skills!
- Vishal Rajput




