In python, strings can be created by enclosing the character or the
sequence of characters in the quotes. Python allows us to use single quotes,
double quotes, or triple quotes to create the string.
Python capitalize() method
converts first character of the string into uppercase without altering the
whole string. It changes the first character only and skips rest of the string
unchanged.
Str
= javatpoint
Print(“Old
value: “, Str)
print(“New value: ”,Str.capitalize())
Old value: javatpoint
New value: Javatpoint
Python Casefold() method
returns a lowercase copy of the string. It is more simillar to lowercase method
except it revomes all case distinctions present in the string.
str = “JAVATPOINT”
# Calling function
str2 = str.casefold()
# Displaying result
print(“Old value:”, str)
print(“New value:”, str2)
Output:
Old value: JAVATPOINT
New value: javatpoint
str = “JAVATPOINT”
# Calling function
str2 = str.casefold()
# Displaying result
print(“Old value:”, str)
print(“New value:”, str2)
Output:
Old value: JAVATPOINT
New value: javatpoint
Python center() method
alligns string to the center by filling paddings left and right of the string.
This method takes two parameters, first is a width and second is a fillchar
which is optional. The fillchar is a character which is used to fill left and
right padding of the string.
center(width[,fillchar])
str = “Hello Javatpoint”
center(width[,fillchar])
str = “Hello Javatpoint”
# Calling function
print(len(str))
str2 = str.center(20)
# Displaying result
print(“Old value:”, str)
print(“New value:”, str2)
Output
16
Old value: Hello Javatpoint
New value: Hello Javatpoint
Output
16
Old value: Hello Javatpoint
New value: Hello Javatpoint
It returns the number of occurences of
substring in the specified range. It takes three parameters, first is a
substring, second a start index and third is last index of the range. Start and
end both are optional whereas substring is required.
# Python count() function example
# Python count() function example
# Variable declaration
str = “Hello Javatpoint”
str2 = str.count('t')
# Displaying result
print(“occurences:”, str2)
Output:
occurences: 2
Output:
occurences: 2
Python encode() method encodes the
string according to the provided encoding standard. By default Python strings
are in unicode form but can be encoded to other standards also.
Encoding is a process of converting text from one standard code to another.
# Python encode() function example
# Variable declaration
str = “HELLO”
encode = str.encode()
# Displaying result
print(“Old value”, str)
print(“Encoded value”, encode)
Output:
Old value HELLO
Encoded value b 'HELLO'
Encoding is a process of converting text from one standard code to another.
# Python encode() function example
# Variable declaration
str = “HELLO”
encode = str.encode()
# Displaying result
print(“Old value”, str)
print(“Encoded value”, encode)
Output:
Old value HELLO
Encoded value b 'HELLO'
Python endswith() method
returns true of the string ends with the specified substring, otherwise returns
false.
endswith(suffix[, start[, end]])
endswith(suffix[, start[, end]])
# Python endswith() function example
# Variable declaration
str = “Hello this is javatpoint.”
isends = str.endswith(“is”,10)
# Displaying result
print(isends)
Output:
False
# Variable declaration
str = “Hello this is javatpoint.”
isends = str.endswith(“is”,10)
# Displaying result
print(isends)
Output:
False
# Python endswith() function example
# Variable declaration
str = “Hello this is javatpoint.”
isends = str.endswith(“is”,0,13)
# Displaying result
print(isends)
Output:
True
# Variable declaration
str = “Hello this is javatpoint.”
isends = str.endswith(“is”,0,13)
# Displaying result
print(isends)
Output:
True
Python expandstabs() method
replaces all the characters by sepecified spaces. By default a single tab
expands to 8 spaces which can be overridden according to the requirement.
We can pass 1, 2, 4 and more to the method which will replace tab by the these number of space characters.
We can pass 1, 2, 4 and more to the method which will replace tab by the these number of space characters.
# Python endswith() function example
# Variable declaration
str = “Welcome \t to \t the \t Javatpoint.”
# Calling function
str2 = str.expandtabs(1)
str3 = str.expandtabs(2)
str4 = str.expandtabs(4)
# Displaying result
print(str2)
print(str3)
print(str4)
# Variable declaration
str = “Welcome \t to \t the \t Javatpoint.”
# Calling function
str2 = str.expandtabs(1)
str3 = str.expandtabs(2)
str4 = str.expandtabs(4)
# Displaying result
print(str2)
print(str3)
print(str4)
Output:
Welcome to the Javatpoint.
Welcome to the Javatpoint.
Welcome to the Javatpoint.
Python find() method finds
substring in the whole string and returns index of the first match. It returns
-1 if substring does not match.
Syntax
find(sub[, start[, end]])
Parameters
sub : substring
start : start index a range
end : last index of the range
Return Type
If found it returns index of the
substring, otherwise -1.
Python String find() Method Example
An example of simple find method which
takes only single parameter (a substring).
# Python find() function example
# Variable declaration
str = “Welcome to the
Javatpoint.”
# Calling function
str2 = str.find(“the”)
# Displaying result
print(str2)
Output:
11
Python String find() Method Example
It returns -1 if not found any match,
see the example.
# Python find() function example
# Variable declaration
str = “Welcome to the
Javatpoint.”
# Calling function
str2 = str.find(“is”)
# Displaying result
print(str2)
Output:
-1
Python String find() Method Example
# Python find() function example
# Variable declaration
str = “Welcome to the
Javatpoint.”
# Calling function
str2 = str.find(“t”)
str3 = str.find(“t”,20,25)
# Displaying result
print(str2)
print(str3)
Output:
8
24
Python format() method is
used to perform format operations on string. While formatting string a
delimiter {} (braces) is used to replace it with the value. This delimeter
either can contain index or positional argument.
Syntax
format(*args, **kwargs)
Parameters
*args : substring
**kwargs : start index a range
Return Type
It returns a formatted string.
Python String format() Method Example
The delimiter (braces) are using
numerical index to replace and format string.
# Python format() function example
# Variable declaration
str = “Java”
str2 = “C#”
# Calling function
str3 = “{1} and {0} both are
programming languages”.format(str,str2)
# Displaying result
print(str3)
Output:
C# and Java both are programming
languages
Python index() method is same
as the find() method except it returns error on failure. This method returns
index of first occurred substring and an error if there is no match found.
Syntax
index(sub[, start[, end]])
Parameters
sub : substring
start : start index a range
end : last index of the range
Return Type
If found it returns an index of the
substring, otherwise an error ValueError.
Python String index() Method Example
# Python index() function example
# Variable declaration
str = “Welcome to the
Javatpoint.”
# Calling function
str2 = str.index(“at”)
# Displaying result
print(str2)
Output:
18
Python String index() Method Example
An error is thrown if the substring is
not found.
# Python index() function example
# Variable declaration
str = “Welcome to the Javatpoint.”
# Calling function
str2 = str.index(“ate”)
# Displaying result
print(str2)
Output:
ValueError: substring not found
Python String index() Method Example
We can also pass start and end index as
parameters to make process more customized.
# Python index() function example
# Variable declaration
str = “Welcome to the
Javatpoint.”
# Calling function
str2 = str.index(“p”,19,21)
# Displaying result
print(“p is present at
:”,str2,”index”)
Output:
p is present at : 20 index
Python isalnum() method checks
whether the all characters of the string is alphanumeric or not. A character
which is either a letter or a number is known as alphanumeric. It does not
allow special chars even spaces.
Syntax
isalnum()
Parameters
No parameter is required.
Return
It returns either True or False.
Python String isalnum() Method Example
If there is a space anywhere in the
string, it returns False. See the example below.
# Python isalnum() function
example
# Variable declaration
str = “Welcome123” # True
str3 = “Welcome 123” # False
# Calling function
str2 = str.isalnum()
str4 = str3.isalnum()
# Displaying result
print(str2)
print(str4)
Output:
True
False
Python isalpha() method
returns true if all characters in the string are alphabetic. It returns False
if the characters are not alphabetic. It returns either True or False.
Syntax
isalpha()
Parameters
No parameter is required.
Return
It returns either True or False.
Python String isalpha() Method Example
# Python isalpha() method example
# Variable declaration
str = “Javatpoint”
if str.isalpha() == True:
print(“String
contains alphabets”)
else: print(“Stringn contains other
chars too.”)
Output:
String contains alphabets
Python isdecimal() method
checks whether all the characters in the string are decimal or not. Decimal
characters are those have base 10.
This method returns boolean either true
or false.
Syntax
isdecimal()
Parameters
No parameter is required.
Return
It returns either True or False.
Python String isdecimal() Method
Example
A simple example of isdecimal() method
to check whether the string contains decimal value.
# Python isdecimal() method
example
# Variable declaration
str = “Javatpoint”
# Calling function
str2 = str.isdecimal()
# Displaying result
print(str2)
Output:
False
Python isdigit() method
returns True if all the characters in the string are digits. It returns False
if no character is digit in the string.
Syntax
isdigit()
Parameters
No parameter is required.
Return
It returns either True or False.
Python String isdigit() Method Example
It returns true only if all the
characters are digit. See the example below.
# Python isdigit() method example
# Variable declaration
str = “12345”
str3 = “120-2569-854”
# Calling function
str2 = str.isdigit()
str4 = str3.isdigit()
# Displaying result
print(str2)
print(str4)
Output:
True
False
Python isidentifier()
method is used to check whether a string is a valid identifier or not. It
returns True if the string is a valid identifier otherwise returns False.
Python language has own identifier
definition which is used by this method.
Syntax
isidentifier()
Parameters
No parameter is required.
Return
It returns either True or False.
Python String isidentifier() Method
Example
Here, we have created variety of
identifiers. Some returns True and some False. See the example below.
# Python isidentifier() method
example
# Variable declaration
str = “abcdef”
str2 = “20xyz”
str3 = “$abra”
# Calling function
str4 = str.isidentifier()
str5 = str2.isidentifier()
str6 = str3.isidentifier()
# Displaying result
print(str4)
print(str5)
print(str6)
Output:
True
False
False
Python string islower()
method returns True if all characters in the string are in lowercase. It
returns False if not in lowercase.
Syntax
islower()
Parameters
No parameter is required.
Return
It returns either True or False.
Python String islower() Method Example
A simple example to understand how to
apply this method. It returns true, see the example below.
# Python islower() method example
# Variable declaration
str = “javatpoint”
# Calling function
str2 = str.islower()
# Displaying result
print(str2)
Output:
True
Python isnumeric() method
checks whether all the characters of the string are numeric characters or not.
It returns True if all the characters are true, otherwise returns False.
Numeric characters include digit
characters and all the characters which have the Unicode numeric value
property.
Syntax
isnumeric()
Parameters
No parameter is required.
Return
It returns either True or False.
Python String isnumeric() Method
Example
See a senario where and how we can
apply isnumeric() method in python programming
# Python isnumeric() method
example
str = “123452500” # True
if str.isnumeric() == True:
print(“Numeric”)
else:
print(“Not numeric”)
str2 = “123-4525-00” # False
if str2.isnumeric() == True:
print(“Numeric”)
else:
print(“Not
numeric”)
Output:
Numeric
Not numeric
Python isprintable() method
returns True if all characters in the string are printable or the string is empty.
It returns False if any character in the string is Nonprintable.
Syntax
isprintable()
Parameters
No parameter is required.
Return
It returns either True or False.
Python String isprintable() Method
Example
Here, we are using some other
characters like newline and tabs in the string. See the results of the example.
# Python isprintable() method
example
# Variable declaration
str = “Hello, Javatpoint”
str2 = “Learn Java here\n”
str3 = “\t Python is a programming
language”
# Calling function
str4 = str.isprintable()
str5 = str2.isprintable()
str6 = str3.isprintable()
# Displaying result
print(str4)
print(str5)
print(str6)
Output:
True
False
False
Python isupper() method
returns True if all characters in the string are in uppercase. It returns False
if characters are not in uppercase.
Syntax
isupper()
Parameters
No parameter is required.
Return
It returns either True or False.
Python String isupper() Method Example
# Python isupper() method example
# Variable declaration
str = “WELCOME TO JAVATPOINT”
# Calling function
str2 = str.isupper()
# Displaying result
print(str2)
Output:
True
Python isspace() method is
used to check space in the string. It returna true if there are only whitespace
characters in the string. Otherwise it returns false. Space, newline, and tabs
etc are known as whitespace characters and are defined in the Unicode character
database as Other or Separator.
Syntax
isspace()
Parameters
No parameter is required.
Return
It returns either True or False.
Python String isspace() Method Example
isspace() method returns true for all
whitespaces like:
' ' - Space
'\t' - Horizontal tab
'\n' - Newline
'\v' - Vertical tab
'\f' - Feed
'\r' - Carriage return
# Python isspace() method example
# Variable declaration
str = “ “ # string contains space
if str.isspace() == True:
print(“It
contains space”)
else:
print(“Not
space”)
str = “ab cd ef \n”
if str.isspace() == True:
print(“It
contains space”)
else:
print(“Not
space”)
str = “\t \r \n”
if str.isspace() == True:
print(“It
contains space”)
else:
print(“Not
space”)
Output:
It contains space
Not space
It contains space
No comments:
Post a Comment