String Practice Question( DS150423 )

  1. How would you check if each word in a string begins with a capital letter?
  2. List down the methods which removes whitespace from the left, right or both sides of a string.
  3. What is the use of title() method.
  4. Write a Python program to calculate the length of a string without using len().
1 Like

1,

my_string = “This Is A Sample String”
capitalized_words = [word for word in my_string.split() if word[0].isupper()]
if len(capitalized_words) == len(my_string.split()):
print(“All words start with a capital letter”)
else:
print(“Not all words start with a capital letter”)

2,

  • strip(): Removes whitespace from the left side of the string.
  • rstrip(): Removes whitespace from the right side of the string.
  • strip(): Removes whitespace from both sides of the string.

3,

my_string = “this is a sample string”
capitalized_string = my_string.title()
print(capitalized_string)

4,

my_string = “This is a sample string”
count = 0
for char in my_string:
count += 1
print(count)

# 1)

some_string = 'This Is A Title String'
print(some_string.istitle())

# A in built function is there called .istitle() that will check if the given 
# string is in a title format or not and returns us a boolean value


# 2)
# lstrip()==>This function removes the whitespaces from left side of the string
# rstrip()==>This function removes the whitespaces from right side of the string
# strip()==>This function removes the whitespaces from both sides of the string


# 3)
# The use of title() method is to Capiltalize ( Capitalize != Upper case) all
# the words of the string

# 4)
some_string = input('Enter your string:   ')
print(some_string.istitle())
length = 0
for i in some_string :
    length += 1

print(length)

Question 1 :

name = "my name is Yogesh."
convert = name.title()
print(convert)
check1 = name.istitle()
print(check1)
check2 = convert.istitle()
print(check2)

Screenshot (345)

Question 2 :

name = "    my name is Yogesh.      "
left_space = name.lstrip()
print(left_space)
right_space = name.rstrip()
print(right_space)
both_spaces = name.strip()
print(both_spaces)

Screenshot (346)

Question 3 :
It is used to capitalize first letter of every word in a string.
Example :

name = "my name is Yogesh."
convert = name.title()
print(convert)

Screenshot (347)

Question 4 :

name = "my name is Yogesh."
count = 0
for i in name:
    count += 1
print(count)

Screenshot (348)

Any_string = “This Is A Sample String”
capitalized_words = [words for word in any_string.split() if word[0].isupper()]
if len(capitalized_words) == len(any_string.split()):
print(“All words start with a capital letter”)
else:
print(“Not all words start with a capital letter”)

strip(): Removes whitespace from the left side of the string.
rstrip(): Removes whitespace from the right side of the string.
strip(): Removes whitespace from both sides of the string.

3.capitalized_string = my_string.title()
print(capitalized_string)

Any_string = “This is a sample string”
count = 0
for char in my_string:
count += 1
print(count)

str1 = “welcome to Edyoda”
print(“The given string is”)
print(str1)
print(“Checking if the first character is Capital or not”)
if (str1[0].isupper()):
print(“The first letter is a capital letter”)
else:
print(“The first letter is not a capital letter”)

strip([chars]): Trims characters from both ends of a string. When chars is None, returns a new string with all leading and trailing whitespace removed.
rstrip([chars]): Trims characters from the right side of a string. When chars is None, returns a new string with all trailing whitespace removed.
lstrip([chars]): Trims characters from the left side of a string. When chars is None, returns a new string with all leading whitespace removed.

The title() method returns a string where the first character in every word is upper case. Like a header, or a title.
If word contains a number or a symbol,the first letter after that will be converted to upper case.

string=raw_input(“Enter string:”)
count=0
for i in string:
count=count+1
print(“Length of the string is:”)
print(count)