Skip to the content.

Day 21: Exploring String and Integer Operations in Python

Task: Understand and practice fundamental string and integer operations.

Description:
String and integer operations are foundational concepts in Python. They allow you to manipulate text data and perform calculations effectively. Today’s task is to explore key string and integer operations by observing their results.


String Operations:

Operations to perform:

  • len(): Get the length of a string.
  • lower(): Convert all characters in a string to lowercase.
  • upper(): Convert all characters in a string to uppercase.
  • replace(): Replace a substring with another substring.
  • find(): Find the index of the first occurrence of a substring.
  • split(): Split a string into a list using a delimiter.
  • join(): Join a list of strings into a single string with a delimiter.
  • isdigit(): Check if the string contains only numeric characters.

Example Expected Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Original string: "  Hello, Python!  "

Length of string: 17

Lowercase: "  hello, python!  "

Uppercase: "  HELLO, PYTHON!  "

Stripped: "Hello, Python!"

Replaced "Python" with "World": "Hello, World!"

Find "Python": 7

Split by ",": ['Hello', ' Python!']

Is "123" numeric?: True

Formatted string: "Welcome, John!"

Integer Operations:

Operations to perform:

  • abs(): Get the absolute value of an integer.
  • max(): Find the maximum value among integers.
  • min(): Find the minimum value among integers.
  • sum(): Calculate the sum of a list of integers.
  • round(): Round an integer or float to the nearest value.
  • pow(): Raise an integer to a power.

Example Expected Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
Original integers: [3, -5, 7, 2]

Absolute of -5: 5

Maximum: 7

Minimum: -5

Sum: 7

Round 5.678: 6

Power 2^3: 8

Conclusion:

Mastering these basic string and integer operations is essential for effective programming in Python. These operations not only make data manipulation easier but also set the foundation for solving more complex problems. By practicing these methods, you build a robust skill set for text processing and numerical computations. Keep exploring and experimenting!