Sie sind auf Seite 1von 2

----------------STRINGS----------------

## A sequence of characters.

## Declare string -

' some text '


" some text "
''' some text '''
""" some text """
' some text's ' - Error
' some text"s '
" some text"s " - Error
" some text's " - Error
''' some text's '''
""" some text"s """
' some text\'s '
" some text\" "

## multi line str literals -


''' '''
""" """

## Access chars of str --


1). Index
2). Slice operator

## Indexing --

+ve - left to right, forward direction


-ve - right to left, backward direction

-6 -5 -4 -3 -2 -1
P y t h o n
0 1 2 3 4 5

## Slicing --

slice = [begin:end:step]
runs from begin to end-1 with given step.

default values -
begin - 0
end - length of str
step - 1

forward - s[:] , s[::]


backward - s[::-1]

## Mathematical ops ---


+ = concatination (both args should be string)
* = string repetation op (one agr is str andother is int)

## Membership operators
in , not in
## Comparision operators
>,<,>=,<=
these operators always check dictionary order(unicode) NOT LENGTH
a - 97 (bigger)
A - 67 (smaller)

## Equality operators
==, !=

## Difference bw == and is
== - if values are same (content comparision)
is - if both vars point to same obj (add/ref compresion)

## Methods and functions -

len(str) - tells length of string


str.strip() - remove spaces
str.lstrip() - remove beginning spaces
str.rstrip() - remove end spaces
str.find('chr') - finds the index of chr, returns -1 if the chr is not
available
str.index('chr') - finds index of chr, gives Error if not found.
str.rfind() - reverse find
str.rindex() - reverse index
str.count(substring) - counts the number of times a substring is
available in str
str.count(substring, begin, end)
str.replace('abc','xyz') - replaces 'abc' with 'xyz'
str.split() - splits string with given arg, default argument is SPACE
'seperator'.join() - joins groups of strs with given seperator
str.upper() - to upper case
str.lower() - to lower case
str.swapcase() - changes case of characters
str.title() - fir char of every word is uppercase
str.capitalize() - only first char is uppercase
str.startswith() - check if str starts with a particular chr/str
str.endsswith() - check if str ends with a particular chr/str
str.isalnum() - checks if str is alphanumeric
str.isalpha() - checks if str is alphabet
str.isdigit() - checks if str is number
str.islower() - checks if str is in lowercase
str.isupper() - checks if str is in uppercase
str.isspace() - checks if str is a space
chr(97) - returns chr of the unicode
ord(char) - returns unicode value of a chr

Das könnte Ihnen auch gefallen