A string is nothing but a series of characters.
Anything inside quotes is considered a string in Python, and you can use single (‘’) or double quotes (“”) around your strings like this:
“Hello String”
‘Hello String’This flexibility will help us to use quotes and apostrophes within our strings:
“I love ‘Python’ ”
‘I said, “I love Python”’
- concatenate String using , and +
# using ','(Used with all type)
print(a,"concatenated with",b)
#using '+' (Only using with String)
print(a+" concated with "+b)
- Adding Whitespace to Strings with Tabs or Newlines
print(“I am good at \n \tC\n\tJava\n\t.Net”) #\n used to start tying from new line and \t used to add space
- String Function.
a = “ python is my world ” #a is a string
title() - where each word begins with a capital letter. (a.title())
upper() - string to all uppercase (a.upper())
lower() - string to all lowercase(a.lower())
strip() - remove whitespaces(a.trip())
lstrip() - strip whitespace from the left side of a string (a.lstrip())
rstrip() - strip whitespace from the right side of a string(a.rstrip())
str() - convert the numerical value to string(str())
capitalize(),title(),upper(),lower(),casefold(),swapcase
capitalize()
it only capitalize first character of the first word from the given string.
if the 1st character is number of the first word then this method doestn't capitalize second character
title()
it capitalize first character of every word from the given string.
if the 1st character is number from any word in the string it will capitalize 2nd character.
str = "i am using python"
print(str.capitalize()) #I am using python
print(str.title()) #I Am Using Python
str = "i am using 'python'"
print(str.capitalize()) #I am using 'python'
print(str.title()) #I Am Using 'Python'
str = "i want 5g"
print(str.capitalize()) #I want 5g
print(str.title()) #I Want 5G
str = "5g"
print(str.capitalize()) #5g
print(str.title()) #5G’
casefold()
lower()
used to convert a given string into lowercase.
str = "ß" #germanword
print(str.casefold()) #ss
print(str.lower()) #ß
str1 = "ss"
print(str.casefold() == str1.casefold()) #ss
print(str.lower() == str1.lower()) #ß
str = "I am Suraj"
print(str.lower())#i am suraj
upper()
used to convert a given string into upper case.
print(str.upper())#I AM SURAJ
swapcase()
converts all uppercase characters to lowercase and all lowercase characters to uppercase characters of the given string, and returns it.
print(str.swapcase())#i AM sURAJ
centre()
given string padded with specified character.
If you don't provide a specified character. It will take whitespace.
centre(width,char) #char is optional
str = "banahhh"
print(str.center(60,"*"))#if the space not contains in the given string then this method added extra char (i.t '*') at right side of the string
str = "ban ahhh"
print(str.center(60,"*"))#if the space contains in the given string then this method added extra char (i.t '*') at left side of the string
str = "banahhh"
print(str.center(60))
count()
return number of occurrence of a substring from given string
case sensitive "i" and "I" is different
count(string,startIndex,endIndex) #startIndex,endIndex are optional
str = "I like sandwich, I like pizza"
subString = "I"
print("Substring count for {} is {}".format(subString,str.count(subString)))
str = "I like sandwich, I like pizza"
subString = "i"
print("Substring count for {} is {}".format(subString,str.count(subString)))
str = "I like sandwich, I like pizza"
subString = "lik"
print("Substring count for {} is {}".format(subString,str.count(subString)))
str = "I like sandwich, I like pizza"
subString = "sand "
print("Substring count for {} is {}".format(subString,str.count(subString)))
str = "I like sandwich, I like pizza"
subString = "i"
print("Substring count for {} is {}".format(subString,str.count(subString,3,28)))
str = "I like sandwich, I like pizza"
subString = "i"
print("Substring count for {} is {}".format(subString,str.count(subString,4,28)))
str = "I like sandwich, I like pizza"
subString = "i"
print("Substring count for {} is {}".format(subString,str.count(subString,400,288)))
endswith()
return true if the given string end with specified suffix otherwise return false
In suffix we provide string or tuple.
endswith(suffix,startindex,endindex)#startIndex,endIndex are optional
print(str.endswith("za"))#True
print(str.endswith(("za","ch","ke"),7,15))#True
startswith()
return true if the given string end with specified prefix otherwise return false
In prefix we provide string or tuple.
startswith(prefix,startindex,endindex)#startIndex,endIndex are optional
print(str.startswith("sa"))#False
print(str.startswith(("sa","ch","ke"),7,15))#True
expandtabs()
All tab characters '\t' replaced with whitespace characters until the next multiple of tabsize parameter.
extpandtabs(tabsize) //tabsize is optional by default it is 8
8,16,24,32,40..
All\tT At the time of count \t position count prevoius \t == number of spaces added in prevoius \t
str = "All\tWe are good\tIn\tPython"
print(str.expandtabs())
print(len(str.expandtabs()))#"All We are good In Python"
print(len("All We are good In Python"))
print(str.expandtabs(3))#All we are good In Python
print(len(str.expandtabs(3)))
print(len("All we are good In Python"))
encode()
returns the encoded version of the given string.
Since Python 3.0, strings are stored as Unicode, i.e. each character in the string is represented by a code point. So, each string is just a sequence of Unicode code points.
For efficient storage of these strings, the sequence of code points is converted into a set of bytes. The process is known as encoding.
encode(encoding='UTF-8',errors='strict') #encoding and errors are optional
print(str.encode(encoding='UTF-8',errors='strict'))
print(str.encode())
find()
returns the index of first occurrence of the substring (if found). If not found, it returns -1
find(str,sInd,eInd) #sInd,eInd are optional
if str.find("are") != -1:
print("substring is found")
else:
print("substring not found")
if str.find("on") != -1: #how to found check this one found accurate word
print("substring is found")
else:
print("substring not found")
if str.find("on",1,0) != -1: #how to found check this one found accurate word
print("substring is found")
else:
print("substring not found")
strip()
This method returns the string by removing both the leading and the trailing characters that are given as an input.
strip([char])#[char] is optional by default it will take whitespace( ) And remove all leading and trailing whitespaces.
str = " Hello I am Chaitanya "
print(str.strip())
#Q what will be the output for below two stmt
print(str.strip("ya"))
print(str.strip(" ya"))#bcuz not have whitespace. try with some other character 8,9 at starting position
str = "I am chaitanya"
print(str.strip("i"))
print(str.strip("I")) #py is case sensitive
lstrip()
This method returns the string by removing the leading characters that are given as an input.
lstrip([char])#[char] is optional by default it will take whitespace( ) And remove all leading whitespaces.
rstrip()
This method returns the string by removing the trailing characters that are given as an input.
rstrip([char])#[char] is optional by default it will take whitespace( ) And remove all trailing whitespaces.
isalpha()
This method return true if all character in a string are alphabets otherwise return false.
In alphabet includes a-z and A-Z
What if some other languages character are coming like french,marathi,telugu,hindi,tamil
isalpha()
str = "Chaitanya"
print(str.isalpha())
str = "My name is Chaitanya"
print(str.isalpha())
str = "ā"
print(str.isalpha())
str = "आना"
print(str.isalpha())
isalnum()
#This method return true if all character in a string are alphanumric otherwise return false.
#In alphabet includes a-z and A-Z and number includes 0-9
#What if some other languages character are coming like french,marathi,telugu,hindi,tamil
#isalnum()
str = "Chaitanya07"
print(str.isalnum())
str = "-1"
print(str.isalnum())
str = "ā"
print(str.isalnum())
str = "आना"
print(str.isalnum())
isdecimal()
#This method returns True if all characters in a string are decimal. If not, it returns False.
#isdecimal()
str = "123"
print(str.isdecimal())
str = "0x123"
print(str.isdecimal())
str = "१२"
print(str.isdecimal())
isdigit()
#This method returns True if all characters in a string are degit. If not, it returns False.
#isdigit()
str = "123"
print(str.isdigit())
str = "-123"
print(str.isdigit())
str = "१२"
print(str.isdigit())
str = "0"
print(str.isdigit())
isidentifier()
#This method returns True if the string is a valid identifier otherwise It returns False.
#isidentifier()
str = "0"
print(str.isidentifier())
str = "asr"
print(str.isidentifier())
str = "#asr"
print(str.isidentifier())
str = "_asr"
print(str.isidentifier())
islower()
#This method returns True if all characters in a string are lower case. If not, it returns False.
#islower()
str = "aAA"
print(str.islower())
str = "asr"
print(str.islower())
isupper()
#This method returns True if all characters in a string are upper case. If not, it returns False.
#isupper()
str = "ASDE"
print(str.isupper())
str = "aSr"
print(str.isupper())
isnumeric()
#This method returns True if all characters in a string are numeric. If not, it returns False.
#You can write the digit and numeric characters using unicode in the program. For example:
# s = '½'
s = '\u00BD'
#isnumeric()
str = "343"
print(str.isnumeric())
print(s.isnumeric())
s = '\u00AA'
print(s.isnumeric())
s = 'SS12'
print(s.isnumeric())
I will explain the difference with an example:
Suppose you have a string, str1 = 'a b 2w' and you want to capitalize all the first characters but if the first character is a digit then you do not want to change. Desired output -> A B 2w
If you do str1.title() it will result in this -> A B 2W and str1.capitalize() will give the following result -> A b 2w
To get the desired result, you have to do something like this:
for x in str1.split():
str1 = str1.replace(x, x.capitalize())