Alphalearningschool
        

<<< Prev

Next >>>

Up
Python String

  • String is popular data type in python. In python we can create a string by double quotes and python also allow the single quotes or triple quotes to create a string in python
    • Str=”Hi How are you” // Hi How are you
    • Str =’Hi how are you’ // Hi how are you
    • Str=” ’Hello’ ” // ’Hello’
    • Print (str) to print the data.

Find the index of string in Python:

str="alphalearning";
print(str[0]) //Find Character of 0th index
print(str.__len__()) // Find the Length of given string
print(len(str)) // Other way find the string the length
print(str.upper()) // Convert the upper case of all string latter
print(str.lower()) //Convert the lower case of all string latter
print(str[0:5]) // Slicing the string in particular rang
print(str[:5]+" World ") // Adding new string in existing string
print("------------------------")
str1=" Qualde"
print(str1.strip()) // Remove the white-space from the string
print(str1.replace("Q","E")) // Replace the character of string
print(str1.split(",")) // Spilt the string
print("------------------------")
str2=input("Enter your String : ") //Taking input from user
print("Length of string is : ",len(str2)) // find the length of taken input


O/P-
O/p
a
13
13
ALPHALEARNING
alphalearning
alpha
alpha World 
------------------------
Qualde
 Eualde
[' Qualde']
------------------------
Enter your String : hello
Length of string is :  5

 

String Operator: 

Operator

Description

Example

+

It use concatenation the value of two strings

 

str=”abc”

Print(str,” –efg ”) // abc –efg

*

Using for crating multiple string.

str=”abc”

print(str*2)  //abcabc

[]

Slice will give exact character from given index

 str[0]  // a

[ : ]

Range Slice it will give you particular character in rang

str[0:1] // ab

in

This “in” use in loop it has return true if character exist in string

a in str will give 1

not in

This “not in” use in loop it has return true if character exist in string

d not in str will give 1

r/R

r mean string will be a raw string. When r or R prefix is present

print r'\n' prints \n and print R'\n'prints \n

%

For string formatting

See at next section

 

Escape character in String:

 

Backslash notation

Hexadecimal character

Description

\a

0x07

 alert

\b

0x08

Backspace

\cx

 

Control-x

\C-x

 

Control-x

\e

0x1b

Escape

\f

0x0c

Form feed

\M-\C-x

 

Meta Control-x

\n

0x0a

New line

\nnn

 

Octal notation, when n is come in the range 0.7

\r

0x0d

Carriage return

\s

0x20

Space

\t

0x09

Tab

\v

0x0b

Vertical tab

\x

 

Character x

\xnn

 

Hexadecimal notation, where n is in the range 0.9, a.f, or A.F