top of page

Strings

Anything we write inside a single quotes or double quotes in python will be consider as strings. 

Create a string

a = 'TechArena'

b = "LearningHub"

print(a)

print(b)

Check the data type

a = 'TechArena'

b = "LearningHub"

print(type(a))

print(type(b))

It makes no difference either we write a string in single quotes or double quotes.

Check the length of a string.

b = "LearningHub"

print(len(b))

String Sciling

String Slicing is use to slice the certain part of a string. which we had already learned in the Slicing part. If you wish to revise the topic. Click on the below button.

Slice the string

b = "TechArena"

print(b[2:7])

String Addition

We can join two or more strings using the addition operator.

Add two Strings

a = "TechArena"

b = "LearningHub"

print(a+b)

String Format

We can format string using the format method or f string

Using f string

a = "TechArena"

b = "LearningHub"

print(f"{a} is the {b} where students learn and grow.")

Using Format Method

a = "TechArena"

b = "LearningHub"

print("{} is the {} where students learn and grow.".format(a,b))

Multiline String

Using three double quotes or three single quotes we can create a multiline string.

Create a multiline string.

a = """

      TechArena is best for overall growth and

      development of career in IT Industry. It prepares

      you for the jobs and guide you through the 

      interview process.

      """

print(a)

Iterate String

You can iterate over a string using a for loop.

Iterate over a string.

a = "TechArena"

for i in a:

    print(i)

String Methods

String methods are use to modify and manipulate strings according to our need. We are going to discuss few of the important string methods.

upper()

Convert all the values to uppercase.

a = "TechArena"

print(a.upper())

lower()

Convert all the values to lowercase.

a = "TechArena"

print(a.lower())

replace()

Syntax : replace(old, new, count)

count is optional parameter. If count is not given then it will replace all the occurance.

replace all the occurence.

a = "TechArenaLearningHub"

print(a.replace('e','x'))

replace only first two occurence.

a = "TechArenaLearningHub"

print(a.replace('e','x',2))

index()

  • Index method will search it from the left side and return the index number of the first occurrence of the specified character.

  • It will throw an error if the specified character doesn't exists in a string.

return the index from left.

a = "TechArena"

print(a.index('e'))

rindex()

  • rindex method will search it from the right side and return the index number of the first occurrence of the specified character.

  • It will throw an error if the specified character doesn't exists in a string.

return the index from right.

a = "TechArena"

print(a.rindex('e'))

find()

  • find method will search it from the left side and return the index number of the first occurrence of the specified character.

  • It will not throw an error if the specified character doesn't exists in a string.

  • It will return -1 if the specified character is not present in a string.

return the index from left.

a = "TechArena"

print(a.find('e'))

rfind()

  • rfind method will search it from the right side and return the index number of the first occurrence of the specified character.

  • It will not throw an error if the specified character doesn't exists in a string.

  • It will return -1 if the specified character is not present in a string.

return the index from right.

a = "TechArena"

print(a.rfind('e'))

capitalize()

convert the first character to upper case and rest to lower

a = "techArena"

print(a.capitalize())

split()

  • Syntax : split(separator,no_of_splits)

  • Both the parameters are optional. If we don't pass the values then separator will be whitespace and all the occurrence will be splitted into a list.

Split the string to list.

a = "tech Arena Learning Hub"

print(a.split())

strip()

remove the whitespace from the beginning and end of the string.

a = " TechArena LearningHub "

print(a.strip())

rstrip()

remove the whitespace from the end of the string.

a = " TechArena LearningHub "

print(a.rstrip())

lstrip()

remove the whitespace from the beginning of the string.

a = " TechArena LearningHub "

print(a.lstrip())

swapcase()

Convert the lowercase to uppercase and viceversa

a = "TechArena LearningHub"

print(a.swapcase())

title()

Convert the fist charcter of each word to uppercase and remaining to lowercase.

a = "techArena learninghub"

print(a.title())

count()

  • Syntax : count(character,start,stop)

  • Both the parameters start and stop are optional. If we don't pass the values then the complete string is considered for counting the number of specific character.

return number of times 'e' occurs in a string.

a = "TechArena Learninghub"

print(a.count('e'))

join()

  • Syntax : 'separator'.join(iterable)

  • With the help of a join method, we can join the string from an iterable object.

Join the list.

a = ["Tech","Arena","Learning","Hub"]

print(' '.join(a))

startswith()

Return true with the string starts with specific character.

a = "TechArena LearningHub"

print(a.startswith("T"))

endswith()

Return true with the string ends with specific character.

a = "TechArena LearningHub"

print(a.endswith("b"))

isalpha()

Return true if all the characters are alphabets.

a = "Hello"

print(a.isalpha())

isnumeric()

Return true if all the characters are numeric.

a = "387"

print(a.isnumeric())

isalnum()

Return true if all the characters are alphanumeric.

a = "Hello20"

print(a.isalnum())

islower()

Return true if all the characters are in lowercase.

a = "techarena"

print(a.islower())

isupper()

Return true if all the characters are in uppercase.

a = "TECHARENA"

print(a.isupper())

String Methods

Method
Explanation
lower()
Convert all the character to lowercase
upper()
Convert all the character to uppercase.
swapcase()
Convert all the lowercase character to uppercase character and vice versa
replace()
replace the characters in a string.
index()
return the index number of the first occurrence of the character from left side.
rindex()
return the index number of the first occurrence of the character from right side.
find()
return the first occurrence of character from left and it won't throw an error unlike index.
rfind()
return the first occurrence of character from right and it won't throw an error unlike index.
split()
Split the element to a list.
strip()
remove whitespace from both side of the string.
rstrip()
remove whitespace from right side of the string.
lstrip()
remove whitespace from left side of the string.
capitalize()
convert the first character to upper case and rest to lowercase.
count()
count the number of times a specific character occurs in a string.
title()
convert first character of all the words to uppercase and rest to lowercase.
join()
Join the iterable object to a string.
endswith()
return True if the string ends with a specified character.
startswith()
return True if the string starts with a specified character.
isalpha()
return True if all the character in a string is alphabets.
isnumeric()
return True if all the character in a string is numbers.
isalnum()
return True if all the character in a string is alphanumeric.
isupper()
return True if all the character in a string is in uppercase.
islower()
return True if all the character in a string is in lowercase.
bottom of page