Displacement, Velocity, Acceleration, and Second Derivatives
python (1).pdf
1. STRINGS IN PYTHON
String in any programming language represents a sequence of
characters.Example-’Computer’ has a sequence of
characters-’C’,’o’,’m’,’p’,’u’,’t’,’e’,’r’.
For representing a string in python we use single or double
quotes.Example-
string=’I love Python’
string=”I love Python”
2.
3. MANIPULATING STRINGS
Strings have various applications in python. Strings can be used for storing data,
manipulating it, representing address, name ,sentence etc. any data which is in
the form of text.
A single character in python is considered as a string having length 1.
Example-string1=’a’
print(string1)
4. How to extract individual characters from a string in python?
This can be done by a method called as Indexing.
Square brackets are used for accessing characters in string.
To represent a character’s position we use a number called index which starts from 0 and
ents at n-1(n represents number of characters in a string).
C O M P U T E R
0 1 2 3 4 5 6 7
In the given program we have tried to access character at 3rd position of string1 and for
string 2 character at 5th position.
5.
6. We have negative indexing too where position of the character is determined from
the end of string and indexing starts from -1.
Example-C O M P U T E R
-8 -7 -6 -5 -4 -3 -2 -1
Reversing a string in python-
Reverse of a string can be obtained by writing[::-1].
Example-
7. REVERSING A STRING
For extracting a subset of a string we use the method of slicing. We can extract
the subset by using :.Example-
8. Further string in python is immutable. Means if we want to manipulate a string then a
new string will be created in which the change desired will be reflected. Means we
cannot make changes in an original string. Any change will be made in the new string
created.
Strings are like objects in python which has a set of built in functions which can be used
to manipulate strings.
There are various built in functions in python used to manipulate a string.
We use dot along with the variable name to invoke function.
9. FUNCTIONS USED FOR MANIPULATING STRINGS
Lower()function-used to convert all the characters in a string to lowercase
13. len()-Used to find the string’s length.Spaces are also counted as a part of the
string.
14. find()-This function takes input and returns the index where that string is found in
the current string.If the string is not present in the current string then -1 is returned.
15. SOME OTHER FUNCTIONS
There are various other functions in the string objects which can be used to
manipulate strings in various ways.Other functions are-
replace()-used to replace substring inside a string.
isnumeric()-used to check numeric characters in a string.
index()-used to return index of substring.
Etc.
Various other operations are in strings which are performed on strings.
16. Compare two strings-== operator is used to compare 2 strings.We obtain false
here as the 2 strings are not same(although the text is same but the characters
are case sensitive in python).