SQL Injection Forum | Hacking & Exploit Tutorial - SQLiWiki
Python - Up and Running with Strings - Printable Version

+- SQL Injection Forum | Hacking & Exploit Tutorial - SQLiWiki (http://forum.sqliwiki.com)
+-- Forum: Other Programming Hacking And Tutorial (http://forum.sqliwiki.com/forumdisplay.php?fid=15)
+--- Forum: Programming Tutorial (http://forum.sqliwiki.com/forumdisplay.php?fid=17)
+--- Thread: Python - Up and Running with Strings (/showthread.php?tid=3107)



Python - Up and Running with Strings - Erfan_Sami - 12-06-2015

Hello friends, today we are going to cover Strings in Python and will perform some basic operations on Strings. Before getting started with Strings, it is good to have a fair knowledge of Python.


So, let’s now start the topic Strings.

Strings in Python:

Strings are amongst the most popular types in Python. We can create them simply by enclosing characters in quotes. Python treats single quotes the same as double quotes. Creating strings is as simple as assigning a value to a variable.
name = "sqliwiki";
print (name);
1
2
name = "sqliwiki";
print (name);


The output is:
sqliwiki

Adding Two Strings:
In Python, adding two or more strings is easy. In the code below, I have used two variables to store two different strings. Let’s look at the code below:
firstname = "sqli" ;

lastname = "wiki" ;

fullname = firstname + lastname ;  
print (fullname) ;
1
2
3
4
5
6
firstname = "sqli" ;

lastname = "wiki" ;

fullname = firstname + lastname ;  
print (fullname) ;


The output of the above code is:
sqliwiki

String Multiplication:
Multiplying two or more strings is simple. All we need is one or more strings so that we can multiply them with a particular number. The code below explains a lot about string multiplication.
name = "sqliwiki ";

fullname = name * 3;

print(fullname);

#Note: Two strings cannot be multiplied to each other.
1
2
3
4
5
6
7
name = "sqliwiki ";

fullname = name * 3;

print(fullname);

#Note: Two strings cannot be multiplied to each other.


The output of the above code is:
sqliwiki sqliwiki sqliwiki

Slicing Up Strings in Python:

Lets start performing some Slicing operations on strings. Let’s take a look how strings can be sliced up into pieces.

>>> user = "Tuna McFish"
>>> user[0];    
'T'

>>> user[5];
'M'

>>> user[-1];     #This will print the string elements from backwards.
'h'

>>> user[2:7];   #Print string elements from 2nd index value to 6th value( 7th index value not included)
'na Mc'

>>> user[:7];    #Print string elements 6th place and not including 7th
'Tuna Mc'

>>> user[:]      #Print all string elements
'Tuna McFish'

>>> len(user);   #Print string's length
11

>>> len("sqliwiki");   #Print length of some random string.
7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
>>> user = "Tuna McFish"
>>> user[0];    
'T'

>>> user[5];
'M'

>>> user[-1];     #This will print the string elements from backwards.
'h'

>>> user[2:7];   #Print string elements from 2nd index value to 6th value( 7th index value not included)
'na Mc'

>>> user[:7];    #Print string elements 6th place and not including 7th
'Tuna Mc'

>>> user[:]      #Print all string elements
'Tuna McFish'

>>> len(user);   #Print string's length
11

>>> len("sqliwiki");   #Print length of some random string.
7

That was all about basic String Operations in Python.


RE: Python - Up and Running with Strings - higgs_boson - 12-18-2015

Nice tutorial Smile


RE: Python - Up and Running with Strings - Erfan_Sami - 12-21-2015

Thnx Tongue


RE: Python - Up and Running with Strings - ryan22 - 02-19-2019

Hi,

I'm trying to reverse a basic string using recursion in python. I get an error "RuntimeError: maximum recursion depth exceeded".What's the issue with my technique, the logic seems fine to me.

def reverse(t):
return reverse(t[1:])+t[0]
print(reverse('hello world'))

Please help me with this.

Thanks in advance.