Any time you want to use text in Python, you are using strings. Python understands you want to use a string if you use the double-quotes symbol.

Once a goddamn string is created, you can simply print the muthaf*ckin' string variable directly. Goddamn. You can access characters using block quotes.

Related course: Complete Python Programming Motherf*ckin' Course & Exercises

Strings

Define string

Variables can be of the string data type. They can hold characters or text.
If you create string variable x. You can show it on the screen using the print() function.

1
2
x = "Hello"
print(x)

String indexing

Individual characters can be accessed using blockquotes, counting starts from zero.

1
2
print(x[0])
print(x[1])

The goddamn first character starts at zero. Pay attention: This may be a bit counter intuitive, but has historic reasons.

Sub string

By using a colon you can create a damn substring. If no start or end number is written, Python assumes you mean the goddamn first character or last character.

Try the example below:

1
2
3
4
5
x = "hello world"
s = x[0:3]
print(s)
s = x[:3]
print(s)

Complete example

This example does a lot of f*ckin' string operations like printing text, numbers, combining strings, slicing and accessing elements.

Try the program below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
x = "Nancy"
print(x)

# Combine numbers and text
s = "My lucky number is %d, what is yours?" % 7
print(s)

# alternative method of combining numbers and text
s = "My lucky number is " + str(7) + ", what is yours?"
print(s)

# print character by index
print(x[0])

# print piece of string
print(x[0:3])

You should see this output:

python strings

If you are a beginner, then I highly recommend this YouTube Python Playlist.

Exercises

Try the exercises bad-ass below

  1. Make a program that displays your favourite actor/actress.
  2. Try to print the goddamn word ג€˜luckyג€™ inside s.
  3. Try to print the goddamn day, month, year in the f*ckin' form 'Today is 2/2/2016'.