Python String

Estimated reading: 4 minutes 653 views

Python String Overview

Strings in python are surrounded by either single quotation marks, or double quotation marks. ‘here’ is the same as “here”. You can display a string with the print()
				
					print("Here")
print('Here')
				
			

String Slicing

The Below code will show the slicing and indexing on the String

				
					test = "Hello World"
test2 = "Python Programming"
#print(test[4])
#print(len(test)) # len is a function which will count the number of character inside the String
print(test2[2:7])
				
			

String startswith()

The startswith() method returns True if the string starts with the specified value, otherwise False.

				
					str = "this is string example....wow!!!"
prefix = "This"
print(str.startswith(prefix))
				
			

String endswith()

The endswith() method returns True if the string ends with the specified value, otherwise False.

				
					str = "this is string example....wow!!!";
suffix = "wow!!!";
print(str.endswith(suffix))
				
			

String find()

The find() method returns the index of first occurrence of the substring (if found). If not found, it returns -1.

				
					str1 = "this is strin g example....wow!!!";
str2 = "exam";

print(str1.find(str2))
				
			

String isaplha()

The isalpha() method returns True if all characters in the string are alphabets. If not, it returns False.

The syntax of isalpha() is:

				
					#str = "this";  # No space & digit in this string
#print(str.isalpha()) # True

str = "this is strin g example....wow!!!";
print(str.isalpha()) # False
				
			

String isdigit()

The isdigit() method returns True if all characters in a string are digits. If not, it returns False

				
					#str = "123456g";  # Only digit in this string
#print(str.isdigit()) # True

str = "this is string example....wow!!!";
print(str.isdigit()) # False
				
			

String isupper()

The string isupper() method returns whether or not all characters in a string are uppercased or not.

The syntax of isupper() method is:

				
					str = "THIS IS STRING EXAMPLE....WOW!!!"; 
#print(str.isupper())

str1 = "THIS is string example....wow!!!";
print(str1.isupper())
				
			

String islower()

The islower() method returns True if all alphabets in a string are lowercase alphabets. If the string contains at least one uppercase alphabet, it returns False.

				
					str2 = "hello";
print(str2.islower()) # True
#str3 = "Hello";


#print(str3.islower()) # False
				
			

String lower()

it will convert Uppercase to lower case.

				
					str4 = "THIS IS STRIN G EXAMPLE....WOW!!!";
print(str4.lower())
				
			

String upper()

it will convert Lowercase to Uppercase case.

				
					str5 = "This IS STRin  gG EXamPLE....WOW!!!";
print(str5.upper())
				
			

String replace()

The replace() method replaces each matching occurrence of the old character/text in the string with the new character/text.

				
					str = "this is string example....wow!!! this is really string"
print(str.replace("is", "was"))
print(str.replace("i", "wwwww"))
print(str.replace("is", "was",2))
				
			

Looping the String

The below code will show the Looping the String.

				
					str6 = "Test_String"
for c in str6:
       print(c)
				
			

String split()

The split() method breaks up a string at the specified separator and returns a list of strings.
				
					str = "THIS IS here STRING EXAMPLE....WOW!!!"
print(str.split(" "))
				
			

String eq()

The __eq__ method is used to compare two string , it is case senstivite

				
					first  =  "first"
second =  "first"
print(first.__eq__(second))
				
			

String __contains__

The __contains__  method is used to check whether the given value in String , Return True if present else return False

				
					str1 = "today"
res1 = str1.__contains__("a")
print(res1)
				
			

String index()

The index() method returns the index of a substring inside the string (if found). If the substring is not found, it raises an exception.

				
					str2 = "today is thursday in india"

print(str2.index("a"))
print(str2.index("ursday"))
				
			

String count()

The count() method returns the number of occurrences of a substring in the given string.

				
					str2 = "today is thursday in india"
print(str2.count('a'))
				
			

String eq()

The __eq__ method is used to compare two string , it is case senstivite

				
					first  =  "first"
second =  "first"
print(first.__eq__(second))
				
			

Leave a Comment

CONTENTS