Python
1: Print Function
2: Escape Sequence
3: Comments
4: Escape Sequence as Normal Text
5: Print Emoji
6: Python as Calculator
7: Strings Concatenation
8: Input
9: int function
10: Variables
11: String Formatting
12: String Indexing
13: String Slicing
14: Step Argument
15: Some useful function and methods
16: Strip Method
17: Find and Replace method
18: Center method
19: If Statement
20: If else statement
21: Nested if-else Statement
22: if-elif-else statement
23: while loop
23.1: Example 1 while loop
23.2: Example 2 while loop
23.3: Example 3 while loop
24: Infinite loop
25: for loop
25.1: Example 1 for loop
26: break and continue statement
27: for loop with string
2: Escape Sequence
3: Comments
4: Escape Sequence as Normal Text
5: Print Emoji
6: Python as Calculator
7: Strings Concatenation
8: Input
9: int function
10: Variables
11: String Formatting
12: String Indexing
13: String Slicing
14: Step Argument
15: Some useful function and methods
16: Strip Method
17: Find and Replace method
18: Center method
19: If Statement
20: If else statement
21: Nested if-else Statement
22: if-elif-else statement
23: while loop
23.1: Example 1 while loop
23.2: Example 2 while loop
23.3: Example 3 while loop
24: Infinite loop
25: for loop
25.1: Example 1 for loop
26: break and continue statement
27: for loop with string
Chapter 17: Find and Replace method
The replace() method is used to replace specific text or characters in a given string with new text or characters.
name = "Free Learning"
namereplace = name.replace("e","E") # replacing all e to E
print(namereplace)
Output: FrEE LEarning
namereplace = name.replace("e","E",2) # replacing first two e to E
print(namereplace)
Output: FrEE Learning
The find() method is used to locate the position (index) of a specified value within a string. If the value is not found, it returns -1.
name = "Free Learning"
namefind_e1 = name.find("e") # finding the position of first e
print(namefind_e1)
Output: 2
namefind_e2 = name.find("e",namefind_e1+1) # finding the position of second e
print(namefind_e2)
Output: 3
namefind_e1 = name.find("M") # Not Found
print(namefind_e1)
Output: -1