String
String variables are enclosed with quotes.
Python uses single quotes '' double quotes "" and triple quotes """
Program illustration on String :
firstName = 'Asish'
lastName = "Samantaray"
app = """Python Tutorial"""
print firstName
print lastName
print app
Copy
Note: Copy and Paste Code into Compiler
Open Compiler
Strings can be used as a whole string, or a part of the variable using brackets [].
Updating Strings :
We can update an existing string by reassigning a variable to another string.Program Illustration :
var = 'Java'
print(var)
var = 'Python' # Reassigning the variable var..
print(var)
Copy
Note: Copy and Paste Code into Compiler
Open Compiler
Escape Characters :
Backslash notation Description
\a Bell or alert
\b Backspace
\cx Control-x
\C-x Control-x
\e Escape
\f Formfeed
\M-\C-x Meta-Control-x
\n Newline
\nnn Octal notation, where n is in the range 0.7
\r Carriage return
\s Space
\t Tab
\ Vertical tab
\x Character x
\xnn Hexadecimal notation, where n is in the range 0.9, a.f, or A.F
Formatting Operator :
%c character
%s string conversion via str
%i decimal integer
%d decimal integerBell
%u unsigned decimal integer
%o octal integer
%x hexadecimal int(lowercase letters)
%X hexadecimal int(UPPERcase letters)
%e exponential notation (with lowercase )
%E exponential notation (with UPPERcase )
%f floating point real number
%g the shorter of %f and %e
%G the shorter of %f and %E
Special Operators :
Assume string variable X holds 'Tech' and variable Y holds 'pro', then −
Operator Description Example
+ Concatenation - Adds values X + Y will give Techpro
* Repetition - Creates new strings X*2 will give -TechproTechpro
[] Slice - Gives the character from the given index X[1] will give T
[ : ] Range Slice - Gives the characters from the given range X[1:4] will give Thh
in Membership - Returns true if a character exists in the given string T in a will give 1
not in Membership - Returns true if a character does not exist in the given string Z not in a will give 1
PreviousNext
String variables are enclosed with quotes.
Python uses single quotes '' double quotes "" and triple quotes """
Program illustration on String :
firstName = 'Asish'
lastName = "Samantaray"
app = """Python Tutorial"""
print firstName
print lastName
print app
Copy
Note: Copy and Paste Code into Compiler
Open Compiler
Strings can be used as a whole string, or a part of the variable using brackets [].
Updating Strings :
We can update an existing string by reassigning a variable to another string.Program Illustration :
var = 'Java'
print(var)
var = 'Python' # Reassigning the variable var..
print(var)
Copy
Note: Copy and Paste Code into Compiler
Open Compiler
Escape Characters :
Backslash notation Description
\a Bell or alert
\b Backspace
\cx Control-x
\C-x Control-x
\e Escape
\f Formfeed
\M-\C-x Meta-Control-x
\n Newline
\nnn Octal notation, where n is in the range 0.7
\r Carriage return
\s Space
\t Tab
\ Vertical tab
\x Character x
\xnn Hexadecimal notation, where n is in the range 0.9, a.f, or A.F
Formatting Operator :
%c character
%s string conversion via str
%i decimal integer
%d decimal integerBell
%u unsigned decimal integer
%o octal integer
%x hexadecimal int(lowercase letters)
%X hexadecimal int(UPPERcase letters)
%e exponential notation (with lowercase )
%E exponential notation (with UPPERcase )
%f floating point real number
%g the shorter of %f and %e
%G the shorter of %f and %E
Special Operators :
Assume string variable X holds 'Tech' and variable Y holds 'pro', then −
Operator Description Example
+ Concatenation - Adds values X + Y will give Techpro
* Repetition - Creates new strings X*2 will give -TechproTechpro
[] Slice - Gives the character from the given index X[1] will give T
[ : ] Range Slice - Gives the characters from the given range X[1:4] will give Thh
in Membership - Returns true if a character exists in the given string T in a will give 1
not in Membership - Returns true if a character does not exist in the given string Z not in a will give 1
PreviousNext
No comments:
Post a Comment