Wednesday, 12 April 2023

String Operation in Python

 Python provides a rich set of built-in functions and methods for performing various operations on strings. Here are some commonly used operations on strings in Python:

  1. Concatenation: To concatenate two or more strings, use the + operator or the join() method.
python
str1 = "Hello" str2 = "World" result1 = str1 + " " + str2 # using the + operator result2 = "".join([str1, " ", str2]) # using the join() method print(result1) # Output: "Hello World" print(result2) # Output: "Hello World"
  1. Substring: To extract a substring from a string, use slicing or the split() method.
python
my_string = "Hello, World!" substring1 = my_string[0:5] # using slicing substring2 = my_string.split(",")[0] # using the split() method print(substring1) # Output: "Hello" print(substring2) # Output: "Hello"
  1. Length: To get the length of a string, use the len() function.
python
my_string = "Hello, World!" length = len(my_string) print(length) # Output: 13
  1. Replace: To replace a substring with another substring, use the replace() method.
python
my_string = "Hello, World!" new_string = my_string.replace("World", "Python") print(new_string) # Output: "Hello, Python!"
  1. Case conversion: To convert a string to uppercase or lowercase, use the upper() and lower() methods.
python
my_string = "Hello, World!" uppercase = my_string.upper() lowercase = my_string.lower() print(uppercase) # Output: "HELLO, WORLD!" print(lowercase) # Output: "hello, world!"

These are just a few of the many operations that can be performed on strings in Python.

Wednesday, 1 March 2023

Tuples Operations in Python Programming

In Python, a tuple is a collection of ordered, immutable elements. This means that once a tuple is created, its elements cannot be modified. However, there are some operations that can be performed on tuples:

  1. Creating a tuple: A tuple can be created by enclosing a sequence of elements in parentheses and separating them with commas. For example:

    python
    my_tuple = (1, 2, 3, 'a', 'b', 'c')
  2. Accessing elements of a tuple: Individual elements of a tuple can be accessed by using indexing. Indexing starts at 0. For example:

    python
    print(my_tuple[0]) # Output: 1
  3. Slicing a tuple: A slice of a tuple can be obtained by using the colon operator (:). For example:

    scss
    print(my_tuple[1:4]) # Output: (2, 3, 'a')
  4. Concatenating tuples: Two tuples can be concatenated using the + operator. For example:

    makefile
    new_tuple = my_tuple + (4, 5, 6)
  5. Multiplying tuples: A tuple can be multiplied by an integer n to create a new tuple with n copies of the original tuple. For example:

    makefile
    repeated_tuple = my_tuple * 3
  6. Finding the length of a tuple: The length of a tuple can be found using the len() function. For example:

    python
    print(len(my_tuple)) # Output: 6
  7. Checking for an element in a tuple: The in operator can be used to check if an element is present in a tuple. For example:

    python
    print('a' in my_tuple) # Output: True
  8. Counting occurrences of an element in a tuple: The count() method can be used to count the number of times an element occurs in a tuple. For example:

    python
    print(my_tuple.count('a')) # Output: 1
  9. Finding the index of an element in a tuple: The index() method can be used to find the index of the first occurrence of an element in a tuple. For example:

    python
    print(my_tuple.index('a')) # Output: 3

Note that since tuples are immutable, operations that attempt to modify the elements of a tuple (such as assignment) will result in a TypeError.

Program For String

 # This program demonstrates various string operations # Define a string variable my_string = "Hello, World!" # Print the string p...