is numeric python

is numeric:

  •  Is numeric is the built-in method that is used to check whether the string contains numbers or not.
  • It returns the true when the string contains numbers, if the string does not contain numbers then it returns false.
  • It is applicable for integers, Unicode values. and takes the exponents like 1/2.
  • It is not applicable for float and negative values.
  •  isnumeric does not have any parameters.
  • For negative values to check whether they are numeric or not we have to remove the negative sign.
syntax:  str.isnumeric()

Example: a="12345"

                 a=str.isnumeric(a)

                 true

#write a python program to demonstrate the isnumeric function.

  1. x="34567"
  2. x=str.isnumeric(x)
  3. print(x)
  4. y="123abc"
  5. y=str.isnumeric(y)
  6. priny(y)
  7. z="asd13434656"
  8. z=str.isnumeric(z)
  9. print(z)
output: true
             false
             false




 

is alpha python

 is alpha:

  • In python "is alpha" is the method used to check the alphabets whether they are present in the given string or substring.
  • In isalpha, it must carry only alphabets.
  • It carries the result in the boolean type
  • If the string contains all alphabets then it returns true, else false.
  • The isalpha is not applicable for the alphanumeric.
  • It is the built-in function of python.
syntax: str.isalpha()

Example:  s="abcdefg"
                   str.isalpha(s)
                    true

#write a python program to demonstrate is alpha function.

  1. a="india"
  2. b="india123"
  3. a=str.isalpha(a)
  4. b=str.isalpha(b)
  5. print(a)
  6. print(b)
output: true
             false


is digit python

is digit() python:  

  • The isdigit()  in python is defined as the string that consists of only digit values. 
  • It returns the result in boolean type if the string has only digits it returns true else false.
  • It accepts the Unicode of that characters also. and also the exponents like ^2.
  • It is not applicable to whitespace, special symbols, alphabets.
Syntax: str.isdigit().

Example:  str="123456"
                 str.isdigit(str) retrns true
Beacuse "123456" is perfectly a digit.
               str=\u00B0
               str.isdigit(str)returns true
Because it takes the Unicode as digit characters.

#write a python code  to demonstrate isdigit() function.

  1. a="7656734"
  2. a=str.isdigit(a)
  3. print(a)
  4. b="2344.60 
  5. b=str.isdigit(b)
  6. print(b)
  7. x="\u00B2"
  8. x=str.isdiigt(x)
  9. print(x)
output: true
            false
            true

2

python substring

  • In python, we have the string concept from that we have a small topic called substring.
  • "substring " is the string that is a part of the main string.
  • In other words, we can say that extraction of the characters from the string.
  • We can access the string by using the index and substring by using the concept of slicing.
  • For the creation of the list of substrings, we use the split().
  • It follows string[start:end: step] in slicing concept by this the substring is formed.
  • startIt is the starting index of the substring. By default, it is 0.
  • end: It is the ending index of the substring. By default, it is the length of the string.
  • step: It is used to increment the index by the given numbers.
  • We use some of the functions in the substrings.
  • In operator: It is used to find the substring that is present in the string.
  • Count(): It is used to count the number of times the substring occurred in the string.It is denoted by the "*" symbol.

#Write a python program to find the substring from a string.

  1. s="hello every one"
  2. print(s[:])
  3. print(s[:5])
  4. print("h" in s)
  5. s=s*3
  6. print(s)

output: hello every one
             hello
              true
              hello every one hello every one hello every one.


python string operations

  • In python, we have some operations that perform the specified operations on strings.
  • In this, the string must enclose with the codes that might be single or double.
  • Here are some python string operations.

1.  String assignment operator:

  • In this operator, the string is assigned to any of the variables.
  • The string may be in single or double codes.
  • It is represented by " =" equal to symbol.
Syntax: string ="any variable."

Example:  string1="python"
                  string2="java"

In the above example, we assign a "python" to string 1 and "java" to string 2 by using the" =" symbol.

# write a python program for the string assignment operator.

  1. str1="welcome to python"
  2. str2="Today topic is string operator"
  3. str3="python is easy  language"
  4. print(str1)
  5. print(str2)
  6. print(str3)
output:  welcome to python
             Today topic is string operator
             Python is easy to language



2. Cancatenate operator:

  • This operator has used to cancate the strings.
  • Concatenation means joining or combing the strings by using the plus symbol between the strings.
  • It is represented by the symbol " +".
       Syntax:  (str1+str2)

       Example:   "Hello"  +"world"
  •  Result is : "Helloworld"

#Write a python program using string cancatenate operator.

  1. str1="a"
  2. str2="is"
  3. str3="alphabet"
  4. print(str1+str2+str3)
output: aisaphabet


3. String repetition operator: 

  • In this operator, the string is repeated the number of times that we declare in the code.
  • It is represented by the symbol "*".
Syntax:  str*n    Here n is the number of times that the string can be repeated.

Example: str="apple"
                 str*2
The result is: apple apple
  • here we declare the string as apple it should be repeated for 2 times.

#Write a python code using the string repetition operator.

  1. str="welcome"
  2. print(str*5)
output: welcome welcome welcome


       

       4.Slicing operator:  

  • In this slicing operator, the strig is divided according to the given index values.
  • The string can y accessed by using the index.
  •  It is having positive index and negative index values.
  • Positive values start from  0 from left to right.
  • The negative index starts from -1 from right to left.
  • [:] this gives all values in the index 0 to end. 
  • The slicing never shows the error, if the index is out of range, it shows an empty list[].  

Syntax: str[]

Example:   str="string"
                   str[0]=s
                   str[1]=t
                   str[-1]=g
  • In the above example, the s index is 0 so it returns s when we write str[0].

#write a python code for slicing operator.

  1. str="coding"
  2. print(str[0])
  3. print(str[-1])
  4. print(str[1:6])
  5. print(str[1:])
  6. print(str[:3])
  7. print(str[1:-4])
  8. print(str[-3])
output:  c
             g
             oding
             oding
             cod
              o
              i                                                                                 


                                       

5. String comparison operator:

  • In the string comparison operator, we compare the strings.
  • In this, we have 2 operator
     1.Equal to(==): If both the strings are the same then it returns true else false.
  • It is represented by the symbol "==".
     Syntax:str1==str2.

       2.not equal(!=): If both strings are not the same then it true else false.
  • It is represented by the symbol "!=".
Syntax:  str1!=str2

#write a python coding using the python string comparison operator.

  1. str1="my name is A"
  2. str2="my name is B"
  3. str3="my name is A"
  4. str4="apple"
  5. str5="apple"
  6. print(str1==str2)
  7. print(str1==str1)
  8. print(str4==str5)
  9. print(str4!=str1)
  10. print(str3!=str2)
output: false
            true
            true
            true
            true



6. Membership operator:

  • In the membership operator, we check the string that is declared in the code is present in the given list or not.
  • There are 2 operators in membership.
  1. In:  It tells that the string is present in the given list.If it is present returns true else false.
  2. Not in: It tells that the string is not present in the given list returns true else false.
Example: str="string"
                r in the str
       The result is: true

#write python code using membership operator.

  1. str="Hello world"
  2. print("H" in str)
  3. print('s' in str)
  4. print("a" not in str)
output:  true
             false
             true



7. Escape sequence operator:

  • This operator is used when an unallowed character is present in the given string.
  • It is represented by the "\" symbol called backslash. 

Write a python program for escape sequence operator.

  1. str="welcome to \"python\" classes"
  2. print(string)
output: welcome to "python" classes





8.String formatting operator:

  • It is used to format the string as per our requirement.
  • There are different types of formats.
  • It is denoted by that "%" symbol.
  • Each format has its own meaning.
%d:  used for sign decimal integer.
%s: used for string.
%f: used for floating.
%u: used for unsigned decimal integer.
%c: used for the character.

#write a python code using the string formatting operator.

  1. name="abc"
  2. marks=78
  3. str1='hi % s'%(name)
  4. print(str1)
  5. str2='hi% s,my marks is %f'%(name,marks)
  6. print(str2)
output: hiabc,my marks is 78.000000


python string methods

 Python string methods:

  • Before going to string methods first of all we have to know what is meant by string.
  • A "String is a set of or group of characters."
  • The string always must be closed with single, double, or triple codes.
  • Triple codes (''' ''') are used for multi-line purposes.
  • In python, we are having some string functions, that are used to operate on strings.
  • As the string is immutable it always returns the new values only. The old values cannot be replaced.
  • The string function is defined as str().

Example:

#1.write python code using the str() methods.

  1. s'"Hello"
  2. print(s[0])
  3. print(s[1])
  4. print(s[2])
  5. print(s[3])
  6. print(s[4])
output:  H
              e
              l 
              l
             o


#2. write a python code using str() functions.

  1. str="Hello"
  2. print(str.lower())
  3. print(str.upper())
  4. print(str.count(str))
output: hello
             HELLO
              1



  • In the following table, we will find more functions and their respective actions in the string.

MethodDescription
len()used to determine the length of the string.
isalnum()used to define the string is alphanumeric.
islower()To define a string is in small letters.
isupper()To define a string is in capital letters.
isnumeric()The string consists of only numbers.
istitle()The string is in the title case.
isspace()The string has only white space.
find()Searches the string value and returns the position.
format()Formats specified values in a string
format_map()Formats specified values in a string
index() returns the position of string where it was found
count()Returns the number of times string occurs.
stratswith() the string starts with the given prefix.
isdecimal() all characters in the string are decimals
isdigit() all characters in the string are digits
isidentifier()Returns True if the string is an identifier
endswith() all characters in the string end with the given suffix. 
encode()it encodes the string.
isprintable()all characters in the string are printable.
capitalize()it returns all the capital strings.
center()Returns the center string. 
_contain_() the string we check is contained in another string or not.
join()Joins the elements of the string.
ljust()Returns a left-justified version of the string
hash()Returns the hash value of the given object.
lstrip()Returns a left trim version of the string
maketrans()Returns a translation table to be used in translations
partition()Returns a tuple where the string is parted into three parts
replace() string where a value is replaced with another value.
rfind()Search the string and returns the last position.
rindex()Search the string and returns the last position.
rjust()Returns a right-justified version of the string
rpartition()Returns a tuple where the string is parted into three parts
rsplit()Splits the string at the specified separator, and returns a list
rstrip()Returns a right trim  of the string
split()Splits the string at the specified separator, and returns a list
splitlines()Splits the string at line breaks and returns a list
id()Returns the identity of the string.
strip() It trims the space between the strings.
swap case()In this, the lower case becomes the upper case and vice versa
title()Converts the first character of each word to upper case
translate()Returns a translated string
upper()Converts a string into the upper case.
zfill()Fills string with a specified number of 0's at the beginning.

Select Menu