String in Python
String in Python
>>> print(s)
Python string
>>> print(s)
>>> print(s)
>>> print(s)
JavaScript tutorial
>>> print(s)
jQuery exercises
JavaScript tutorial
>>> s = 'jQuery exercises\n JavaScript tutorial\n Python tutorial and exercises ...'
>>> print(s)
jQuery exercises
JavaScript tutorial
Python string
>>> b = a[2]
>>> print(b)
>>> a[0]
'P'
>>>
Explanation :
The above statement selects character at position number 2 from a and assigns it to b.
The expression in brackets is called an index that indicates which character we are
interested.
The index is an offset from the beginning of the string, and the offset of the first letter
is zero.
>>> b = a[4+3]
>>> print(b)
s
>>>
>>> a[2.3]
>>>
Negative indices:
Alternatively, we can use negative indices, which count backward
from the end of the
Index -1 gives the last character.
>>> a = "Python string"
>>> a[-2]
'n'
>>> a[-8]
'n'
>>>
Python strings are "immutable" which means they cannot be changed after they are
created
>>> a = "PYTHON"
>>> print(b)
XYTHON
>>> print(a)
PYTHON
>>>
Copy
Python String concatenation:
The '+' operator is used to concatenate two strings.
>>> a = "Python" + "String"
>>> print(a)
PythonString
>>>
>>> b = "Script"
>>> a+=b
>>> print(a)
JavaScript
>>>
>>> print(b)
<PythonStringPythonStringPythonString>
>>>
String length:
len() is a built-in function which returns the number of characters in a
string:
>>> a = "Python string"
>>> len(a)
13
>>> a[13]
>>> a[12]
'g'
>>>
They start at the beginning, select each character, in turn, do something to it, and
continue until the end.
Code:
a = "STRING"
i=0
c = a[i]
print(c)
i=i+1
Output:
>>>
S
T
R
I
N
G
>>>
Traversal with a for loop :
Print entire string on one line using for loop.
Code:
a = "Python"
i=0
new=" "
new = new+b
i=i+1
print(b)
print(new)
Output:
>>>
P
P
y
Py
t
Pyt
h
Pyth
o
Pytho
n
Python
>>>
String slices:
A segment of a string is called a slice. The "slice" syntax is used to
get sub-parts of a string. The slice s[start:end] is the elements
beginning at the start (p) and extending up to but not including end
(q).
The operator [p:q] returns the part of the string from the pth character to the qth
character, including the first but excluding the last.
If we omit the first index (before the colon), the slice starts at the beginning of the
string.
If you omit the second index, the slice goes to the end of the string:
If the first index is greater than or equal to the second the result is an empty string,
represented by two quotation marks.
Example-1
>> s = "Python"
s[1:4] is 'yth' -- chars starting at index 1 and extending up to but not including index 4
s[1:] is 'ython' -- omitting either index defaults to the start or end of the string
s[1:100] is 'ython' -- an index that is too big is truncated down to the string length
Python uses negative numbers to give easy access to the chars at the
end of the string. Negative index numbers count back from the end of
the string:
s[-3:] is 'hon' -- starting with the 3rd char from the end and extending to the end of the
string.
Example-2
>>> a = "Python String"
>>> print(a[6:11])
Stri
>>> print(a[5:13])
n String
>>>
>>> print(a[:8])
Python S
>>> print(a[4:])
on String
>>> print(a[6:3])
>>>
Example-3
>>> a = "PYTHON"
>>> a[3]
'H'
>>> a[0:4]
'PYTH'
>>> a[3:5]
'HO'
...
>>> a[:2]
'PY'
...
>>> a[3:]
'HON'
>>>
L=len(str)
print(L)
i=0
while i < L:
if str[i]== char:
return 1
i=i+1
return -1
print(search("P","PYTHON"))
Output:
>>>
6
1
>>>
It takes a character and finds the index where that character appears in a string.
Another example
def search(char,str):
L=len(str)
print(L)
i=0
while i < L:
if str[i]== char:
return 1
i=i+1
return -1
print(search("S","PYTHON"))
Copy
Output:
>>>
6
-1
>>>