Aug 12, 2019

Playing with Strings | Python programming

A string is a array of characters. Also we can call that as a character buffer. In a previous tutorial we learned how we can print strings. In hear we are going to play more with strings. Read following script and try to understand it. You can simply get an idea what's happening.
[email protected]:~/programming$ cat tmp1.py
#!/usr/bin/env python
x = "www."
y = "HacksLand"
z = ".net"
print (y)
print (x+y+z)

[email protected]:~/programming$ python tmp1.py
HacksLand
www.HacksLand.net

[email protected]:~/programming$ python3 tmp1.py
HacksLand
www.HacksLand.net
We normally indicate a string by adding two ' s or " s around it. We can print a string by directly feed to a print() function as argument or giving a string variable to it. In above example you can see we combined three strings into one. If x,y and z are string variables we can make a single string with x+y+z. Is it possible to combine strings with integers? Why not giving a try.
[email protected]:~/programming$ cat tmp1.py
#!/usr/bin/env python

x = "Now our community have"
y = 1058
z = "members"

print (x+y+z)

[email protected]:~/programming$ python tmp1.py
Traceback (most recent call last):
  File "tmp1.py", line 7, in <module>
    print (x+y+z)
TypeError: cannot concatenate 'str' and 'int' objects
[email protected]:~/programming$ nano tmp1.py
[email protected]:~/programming$ cat tmp1.py
#!/usr/bin/env python

x = "Now our community have "
y = 1058
z = " members"

print (x+ str(y) +z)

[email protected]:~/programming$ python tmp1.py
Now our community have 1058 members
You can clearly see we got an error when we combined a string with an integer. But languages like JavaScript allow us to do this. After I edit is we could run it without errors. What we did is convert the integer to a string using str() function. It's possible to multiply a string and print it.
[email protected]:~/programming$ cat tmp1.py
#!/usr/bin/env python
str = " HacksLand "
print (str * 5 )

[email protected]:~/programming$ python tmp1.py
 HacksLand  HacksLand  HacksLand  HacksLand  HacksLand 
I said that string is a character array. So we can extract an character with an index. In C we can see this effect in deeply.  
[email protected]:~/programming$ cat tmp1.py
#!/usr/bin/env python
str = 'HacksLand' 
print (str[2] )
print (str[1] )
print (str[0] )
# what if we use a negative index
print (str[-2] )
print (str[-1] )

[email protected]:~/programming$ python tmp1.py
c
a
H
n
d
hgfgh
[email protected]:~/programming$ cat tmp1.py
#!/usr/bin/env python
str = 'HacksLand' 
print (str[0:2] )
print (str[3:6] )
print (str[3:] )
print (str[4:] )
print (str[-4:-2] )

[email protected]:~/programming$ python tmp1.py
Ha
ksL
ksLand
sLand
La

[email protected]:~/programming$ python3 tmp1.py
Ha
ksL
ksLand
sLand
La
That's all for this tutorial. See you in next posts. Thanks for reading.

Jun 13
Build A Simple Web shell

A web shell is a piece of code written to get control over a web server. It is helpful for....

Apr 12
C programming variables explained

In computer science you may heard about virtual memory model. In this model computer memory is....

Aug 12
Variables | Python programming

You know that variable is a memory space ant it contains a value. When we talk in low level that's....

Replying to 's comment Cancel reply