Python Document
Python Document
For example:
We use square brackets for slicing along with the index or indices to obtain a
substring.
var1 = "Guru99!"
var2 = "Software Testing"
print ("var1[0]:",var1[0])
print ("var2[1:5]:",var2[1:5])
Suppose if a=guru and b=99 then a+b= "guru99". Similarly, if you are using a*2,
it will "GuruGuru". Likewise, you can use other operators in string.
Operator Description Example
[] Slice- it gives the letter a[1] will give "u" from the word Guru as such x="Guru"
from the given index ( 0=G, 1=u, 2=r and 3=u) print x[1]
[:] Range slice-it gives the x [1:3] it will give "ur" from the word Guru. x="Guru"
characters from the given Remember it will not consider 0 which is G, print x[1:3]
range it will consider word after that is ur.
in Membership-returns true if u is present in word Guru and hence it will x="Guru"
a letter exist in the given give 1 (True) print "u" in x
string
not in Membership-returns true if l not present in word Guru and hence it will x="Guru"
a letter exist is not in the give 1 print "l" not in x
given string
r/R Raw string suppresses Print r'\n' prints \n and print R'/n' prints \n
actual meaning of escape
characters.
% - Used %r - It insert the canonical The output of this code will be "guru 99". name = 'guru'
for string string representation of the number = 99
format object (i.e., repr(o)) %s- It print'%s %d' % (name
insert the presentation string ber)
representation of the object
(i.e., str(o)) %d- it will
format a number for display
+ It concatenates 2 strings It concatenate strings and gives the result x="Guru"
y="99"
print x+y
* Repeat It prints the character twice. x="Guru"
y="99"
print x*2
x = "Hello World!"
print(x[:6])
print(x[0:6] + "Guru99")
string="python at guru99"
print(string.upper())
Likewise, you can also do for other function as well like capitalize
string="python at guru99"
print(string.capitalize())
string="PYTHON AT GURU99"
print(string.lower())
For example, if you want to add a colon (:) after every character in the string
"Python" you can use the following code.
print(":".join("Python"))
Reversing String
By using the reverse function, you can reverse the string. For example, if we
have string "12345" and then if you apply the code for the reverse function as
shown below.
string="12345"
print(''.join(reversed(string)))
Split Strings
Split strings is another function that can be applied in Python let see for string
"guru99 career guru99". First here we will split the string by using the command
word.split and get the result.
To understand this better we will see one more example of split, instead of
space (' ') we will replace it with ('r') and it will split the string wherever 'r' is
mentioned in the string
Important Note:
x = "Guru99"
x.replace("Guru99","Python")
print(x)
x = "Guru99"
x = x.replace("Guru99","Python")
print(x)
Above codes are Python 3 examples, If you want to run in Python 2 please
consider following code.
Python 2 Example
Python has introduced a .format function which does way with using the
cumbersome %d and so on for string formatting.
Summary:
Since Python is an object-oriented programming language, many functions can
be applied to Python objects. A notable feature of Python is its indenting source
statements to make the code easier to read.
• Accessing values through slicing - square brackets are used for slicing
along with the index or indices to obtain a substring.
o In slicing, if range is declared [1:5], it can actually fetch the value
from range [1:4]
• You can update Python String by re-assigning a variable to another string
• Method replace() returns a copy of the string in which the occurrence of
old is replaced with new.
o Syntax for method replace: oldstring.replace("value to
change","value to be replaced")
• String operators like [], [ : ], in, Not in, etc. can be applied to concatenate
the string, fetching or inserting specific characters into the string, or to
check whether certain character exist in the string
• Other string operations include
o Changing upper and lower case
o Join function to glue any character into the string
o Reversing string
o Split string