0% found this document useful (0 votes)
12 views3 pages

string Questions

The document contains various Python coding exercises and their expected outputs. It includes tasks such as string manipulation, counting characters, reversing words, and removing duplicates. Each exercise is followed by example code snippets and their results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

string Questions

The document contains various Python coding exercises and their expected outputs. It includes tasks such as string manipulation, counting characters, reversing words, and removing duplicates. Each exercise is followed by example code snippets and their results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

In [1]: # What will be the output of the following code?

s = "PythonProgramming"
print(s[2:10:2])

toPo

In [2]: # What does the following code output?


s = "hello world"
print(s.capitalize().swapcase())

hELLO WORLD

In [3]: # Write a Python code to count the occurrences of a given character in a string.

name=input("enter your name:")


char= input("enter a letter you want to count:")
count=0
for i in name:
if i == char:
count= count+1
print(count)

enter your name:saurav


enter a letter you want to count:a
2

In [ ]: # What will be the output of the following?


name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")

In [2]: # Write a code to check if a given string is a palindrome (ignoring case and spaces).

In [16]: # Given a string sentence, write a Python program to reverse the words but keep their or
sentence= "Hello how are you"
reverse=[]
sentence= sentence.split()
for i in sentence:
reverse.append(i[::-1])
reverse=" ".join(reverse)
print(reverse)

olleH woh era uoy

In [5]: # Write a Python code to find the longest word in a sentence.


sentence = "Find the longest word in this sentence"
words = sentence.split() # Split the sentence into a list of words

longest_word = "" # Initialize the longest word as an empty string

# Iterate through each word in the list


for word in words:
if len(word) > len(longest_word): # Compare the length of the word with the longest
longest_word = word # Update the longest word

print("The longest word is:", longest_word)

The longest word is: sentence

In [8]: # Write a Python program to remove duplicate characters from a string while maintaining
my_string = "programming"
result = ""
seen = set() # A set to keep track of characters we've already seen

# Iterate through each character in the string


for char in my_string:
if char not in seen:
result += char # Add to result if not seen already
seen.add(char) # Mark the character as seen

print("String after removing duplicates:", result)

String after removing duplicates: progamin

In [6]: # Write a code that compresses a string using the counts of repeated characters.
# Input: aabcccccaaa
# Output: a2b1c5a3
s = "aabcccccaaa"
s1 = ""
count = 1
for i in range(1, len(s)):
if s[i] == s[i - 1]:
count += 1
else:
s1 += s[i - 1] + str(count)
count = 1
s1 += s[-1] + str(count)

print(s1)

a2b1c5a3

In [4]: # Write a Python code that returns the first non-repeating character in a given string.
s= input("enter any word:")
s1=""
for i in s:
if s.count(i)<2:
s1=s1+i
first_np=s1[0]
print(first_np)

enter any word:saurav


s

In [5]: # Find the length of a given string without using the len() function
my_string = "Hello, World!"
count = 0

for char in my_string:


count += 1

print(count) # Output: 13

13

In [18]: # Extract username from a given email.


# Eg if the email is Saurav24Gupta@gmail.com
# then the username should be Saurav24Gupta
email= input("enter your email")
user_id= email.find("@")
user_id= email[:email.find("@")]
print(user_id)

enter your emailSaurav24Gupta@gmail.com


Saurav24Gupta

In [20]: # Count the frequency of a particular character in a provided string.


# Eg 'hello how are you' is the string, the frequency of h in this string is 2.
string= "hello how are you"
char= input("enter a character")
for i in string:
count= string.count(char)
print(count)

enter a charactero
3

In [11]: # Write a program which can remove a particular character from a string.

In [12]: # Write a program to count the number of words in a string without split()

In [13]: # Write a python program to convert a string to title case without using the title()

In [14]: # Write a program that can convert an integer to string.

You are given a string s consisting of lowercase


English letters. Your task is to perform the
following transformation:
Replace every vowel in the string ('a', 'e', 'i', 'o', 'u') with its uppercase form.

Remove all duplicate characters while preserving the original order.

Reverse the final string after processing steps 1 and 2.

In [15]: #"s = "hello"

#o/p= "OlEh"

You are given a string s consisting of lowercase


English letters. Your task is to remove all
characters at odd indices and return the modified
string.
In [17]: # s = "hacker"
# "hce"

In [ ]:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy