string Questions
string Questions
s = "PythonProgramming"
print(s[2:10:2])
toPo
hELLO WORLD
In [3]: # Write a Python code to count the occurrences of a given character in a string.
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)
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
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)
In [5]: # Find the length of a given string without using the len() function
my_string = "Hello, World!"
count = 0
print(count) # Output: 13
13
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()
#o/p= "OlEh"
In [ ]: