Friday 14 April 2023

Program For String

 # This program demonstrates various string operations


# Define a string variable

my_string = "Hello, World!"


# Print the string

print("Original string: " + my_string)


# Get the length of the string

print("Length of string: " + str(len(my_string)))


# Convert the string to uppercase

print("Uppercase string: " + my_string.upper())


# Convert the string to lowercase

print("Lowercase string: " + my_string.lower())


# Replace a substring within the string

new_string = my_string.replace("World", "Python")

print("Replaced string: " + new_string)


# Check if the string starts with a specific substring

if my_string.startswith("Hello"):

    print("The string starts with 'Hello'")

else:

    print("The string does not start with 'Hello'")


# Check if the string ends with a specific substring

if my_string.endswith("World!"):

    print("The string ends with 'World!'")

else:

    print("The string does not end with 'World!'")


# Split the string into a list of words

word_list = my_string.split()

print("List of words in string: ")

for word in word_list:

    print(word)


# Join a list of strings into a single string

joined_string = " ".join(word_list)

print("Joined string: " + joined_string)


# Access individual characters in the string

print("First character: " + my_string[0])

print("Last character: " + my_string[-1])


# Slice a substring from the string

substring = my_string[0:5]

print("Substring: " + substring)


If Else in Python

 In Python, the if-else statement is used for conditional branching. It allows you to execute a block of code if a certain condition is true, and another block of code if the condition is false. The basic syntax of an if-else statement in Python is as follows:

python
if condition: # code to execute if condition is true else: # code to execute if condition is false

Here's an example that demonstrates how to use if-else statement in Python:

python
# a simple program that prints whether a number is positive, negative, or zero num = int(input("Enter a number: ")) if num > 0: print("The number is positive") elif num < 0: print("The number is negative") else: print("The number is zero")

In this example, we first prompt the user to enter a number using the input() function and convert it to an integer using the int() function. Then, we use the if-elif-else statement to check whether the number is positive, negative, or zero. If the number is greater than 0, the code inside the first if block is executed, which prints "The number is positive". If the number is less than 0, the code inside the elif block is executed, which prints "The number is negative". If the number is 0, the code inside the else block is executed, which prints "The number is zero".

Note that the elif keyword is used to add additional conditions to check. You can have as many elif blocks as you need to check different conditions. Also note that the if-else statement can be nested inside other if statements to create more complex conditions.

For Loop in Python

 A for loop in Python is a control flow statement that allows you to iterate over a sequence (e.g., list, tuple, dictionary, string, range) and execute a block of code for each item in the sequence. The basic syntax of a for loop in Python is:

yaml
for item in sequence: # code block to be executed for each item in the sequence

Here, item represents the current item in the sequence being iterated, and sequence represents the iterable object (e.g., list, tuple, etc.) over which you are iterating.

For example, let's say you have a list of numbers and you want to print each number on a new line. You can use a for loop to iterate over the list and print each item:

css
numbers = [1, 2, 3, 4, 5] for number in numbers: print(number)

This will output:

1 2 3 4 5

You can also use the range() function to generate a sequence of numbers and iterate over it using a for loop:

scss
for i in range(5): print(i)

This will output:

0 1 2 3 4

You can combine a for loop with conditional statements (if-else statements) to perform certain actions based on certain conditions

Wednesday 12 April 2023

String Operations Using Their Index

 In Python, you can perform various operations on strings using their index. Here are some common operations:

  1. Accessing a character at a specific index: You can access a character in a string by using its index in square brackets []. For example:

    bash
    my_string = "hello" print(my_string[0]) # Output: h
  2. Slicing a string: You can extract a portion of a string by specifying the starting and ending indices separated by a colon [:]. For example:

    bash
    my_string = "hello world" print(my_string[0:5]) # Output: hello
  3. Reversing a string: You can reverse a string by using the slicing technique with a step value of -1. For example:

    bash
    my_string = "hello" print(my_string[::-1]) # Output: olleh
  4. Finding the index of a character: You can find the index of a character in a string using the index() method. For example:

    bash
    my_string = "hello" print(my_string.index('e')) # Output: 1
  5. Checking if a substring exists in a string: You can check if a substring exists in a string using the in operator. For example:

    bash
    my_string = "hello world" print('world' in my_string) # Output: True
  1. Concatenating strings: You can concatenate two or more strings using the + operator. For example:

    makefile
    my_string1 = "hello" my_string2 = "world" print(my_string1 + my_string2) # Output: helloworld
  2. Repeating a string: You can repeat a string by using the * operator. For example:

    bash
    my_string = "hello" print(my_string * 3) # Output: hellohellohello
  3. Converting a string to a list of characters: You can convert a string to a list of its individual characters using the list() function. For example:

    go
    my_string = "hello" print(list(my_string)) # Output: ['h', 'e', 'l', 'l', 'o']
  4. Counting the occurrences of a substring: You can count the number of occurrences of a substring in a string using the count() method. For example:

    bash
    my_string = "hello world" print(my_string.count('l')) # Output: 3
  5. Finding the last index of a character: You can find the last index of a character in a string using the rindex() method. For example:

    bash
    my_string = "hello" print(my_string.rindex('l')) # Output: 3

These are some more operations on strings using index in Python that you can use to manipulate

Program For String

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