Skip to main content

Assignement python strings set -001

 


Basic String Manipulation

  1. String Length: Write a Python program to find the length of a given string without using the built-in len() function.
  2. String Reversal: Create a function to reverse a string without using the built-in reversed() or slicing techniques.
  3. Character Counting: Write a program to count the occurrences of a specific character in a string.
  4. String Slicing: Given a string, extract a substring from the specified indices.

String Operations and Methods

  1. String Concatenation: Combine two or more strings using different methods (e.g., +, join).
  2. String Formatting: Format a string to display information in a specific pattern using f-strings or other formatting techniques.
  3. String Case Conversion: Convert a string to uppercase, lowercase, or title case.
  4. String Stripping: Remove leading and trailing whitespace from a string.
  5. String Splitting: Split a string into a list of substrings based on a delimiter.
  6. String Searching: Find the index of a substring within a given string.

1. String Length without len()

Python
def string_length(s):
  count = 0
  for char in s:
    count += 1
  return count

# Example usage:
my_string = "Hello, world!"
length = string_length(my_string)
print(length)  # Output: 12

2. String Reversal without Built-in Functions

Python
def reverse_string(s):
  reversed_str = ""
  for i in range(len(s) - 1, -1, -1):
    reversed_str += s[i]
  return reversed_str

# Example    usage:
my_string = "python"
reversed_str = reverse_string(my_string)
print(reversed_str)  # Output: nohtyp

3. Character Counting

Python
def count_char(s, char):
  count = 0
  for c in s:
    if c == char:
      count += 1
  return count

# Example usage:
my_string = "hello world"
char = 'l'
count = count_char(my_string, char)
print(count)  # Output: 3

4. String Slicing

Python
def extract_substring(s, start, end):
  return s[start:end]

# Example usage:
my_string = "Python programming"
substring = extract_substring(my_string, 0, 6)
print(substring)  # Output: Python

5. String Concatenation

Python
def concatenate_strings(str1, str2):
  # Using + operator
  concatenated_str = str1 + str2
  return concatenated_str

# Example usage:
str1 = "Hello"
str2 = "World"
result = concatenate_strings(str1, str2)
print(result)  # Output: HelloWorld

6. String Formatting

Python
name = "Alice"
age = 30
formatted_string = f"Hello, my name is {name} and I am {age} years old."
print(formatted_string)

7. String Case Conversion

Python
my_string = "hElLo, WoRlD"
uppercase_str = my_string.upper()
lowercase_str = my_string.lower()
titlecase_str = my_string.title()
print(uppercase_str)
print(lowercase_str)
print(titlecase_str)

8. String Stripping

Python
my_string = "   This is a string with whitespace   "
stripped_string = my_string.strip()
print(stripped_string)

9. String Splitting

Python
my_string = "apple,banana,orange"
fruits = my_string.split(",")
print(fruits)

10. String Searching

Python
my_string = "I love programming"
index = my_string.find("programming")
print(index)  # Output: 7

Palindrome Check (Challenge)

Python
def is_palindrome(s):
  reversed_str = s[::-1]
  return s == reversed_str

# Example usage:
word = "radar"
if is_palindrome(word):
  print("It's a palindrome!")
else:
  print("It's not a palindrome.")

Comments

Popular posts from this blog

Telecom OSS and BSS: A Comprehensive Guide

  Telecom OSS and BSS: A Comprehensive Guide Table of Contents Part I: Foundations of Telecom Operations Chapter 1: Introduction to Telecommunications Networks A Brief History of Telecommunications Network Architectures: From PSTN to 5G Key Network Elements and Protocols Chapter 2: Understanding OSS and BSS Defining OSS and BSS The Role of OSS in Network Management The Role of BSS in Business Operations The Interdependence of OSS and BSS Chapter 3: The Telecom Business Landscape Service Providers and Their Business Models The Evolving Customer Experience Regulatory and Compliance Considerations The Impact of Digital Transformation Part II: Operations Support Systems (OSS) Chapter 4: Network Inventory Management (NIM) The Importance of Accurate Inventory NIM Systems and Their Functionality Data Modeling and Management Automation and Reconciliation Chapter 5: Fault Management (FM) Detecting and Isolating Network Faults FM Systems and Alerting Mecha...

The Silicon Race: AI Chips and the Future of Competition

  The Silicon Race: AI Chips and the Future of Competition The landscape of Artificial Intelligence (AI) is being reshaped at an unprecedented pace, and at its heart lies a furious competition in the development of specialized AI chips. These miniature marvels, whether powering vast data centers or enabling intelligence on the edge, are the silent workhorses transforming industries, enabling real-time decision-making, and pushing the boundaries of what AI can achieve. The stakes are immense, with the global AI chip market projected to surge from approximately $31.6 billion today to over $846 billion by 2035, highlighting an intense and evolving competitive arena. The Driving Force: Why Specialized AI Chips? Traditional CPUs, the general-purpose workhorses of computing, simply cannot meet the insatiable demands of modern AI workloads. The core operations of machine learning, particularly linear algebra and matrix multiplications, are inherently parallel. This led to the rise of s...

Medical education still in stone age?

## 🚨 ഉണരാനുള്ള സമയം: നമ്മുടെ മെഡിക്കൽ വിദ്യാഭ്യാസം ശിലായുഗത്തിൽ! ഇനി വേണ്ടത് #ടെക്എംബിബിഎസ് ഉം #ടെക്നഴ്സിംഗും! 💉🤖 ചൈനയിലെ **ഡോക്ടർമാരില്ലാത്ത എ.ഐ. കിയോസ്‌കുകളുടെ** (Doctorless AI Kiosks) ഒരു വീഡിയോ ഞാൻ പങ്കുവെക്കുന്നു (ചേർത്തിട്ടുണ്ട്). പ്രാഥമിക ആരോഗ്യ പരിചരണം എത്ര വേഗമാണ് സാങ്കേതികവിദ്യ മാറ്റിമറിക്കുന്നതെന്നതിന്റെ ഞെട്ടിക്കുന്ന ഉദാഹരണമാണിത്. ഇത് ഭാവിയിലേക്കുള്ള കാഴ്ചയല്ല—ഇത് **ഇപ്പോഴത്തെ യാഥാർത്ഥ്യമാണ്**. ആരോഗ്യ സംരക്ഷണ വിദ്യാഭ്യാസത്തിൽ സമൂലമായ മാറ്റം അനിവാര്യമാകുന്ന ഒരു സാങ്കേതിക മുന്നേറ്റത്തിനാണ് നമ്മൾ സാക്ഷ്യം വഹിക്കുന്നത്. എന്നിട്ടും **മെഡിക്കൽ കൗൺസിൽ ഓഫ് ഇന്ത്യ (MCI)** പോലുള്ള സ്ഥാപനങ്ങളും ലോകമെമ്പാടുമുള്ള വിദ്യാഭ്യാസ ബോർഡുകളും ഇപ്പോഴും പഴയ രീതിയിൽ തുടരുന്നു. എന്റെ മകൾ MBBS വിദ്യാർത്ഥിയാണ്. **1000 പേജുള്ള അനാട്ടമി പാഠപുസ്തകം കാണാപ്പാഠം പഠിച്ച്** പരീക്ഷ എഴുതാൻ അവൾ ഇപ്പോഴും നിർബന്ധിതയാവുകയാണ്. എന്നാൽ ലോകമെമ്പാടുമുള്ള AI കാര്യക്ഷമതയുടെ നിലവാരം ഇതാ: * **ഒരു എ.ഐ. ഡോക്ടറിന്** ലോകത്തിലെ എല്ലാ മനുഷ്യ ഡോക്ടർമാരെയും സഹായിക്കാൻ കഴിയും. * **ഒരു റോബോട്ടിക് നഴ്സിന്** 100 മനുഷ്യ നഴ്സു...