Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
322 lines (233 loc) · 7.53 KB

File metadata and controls

322 lines (233 loc) · 7.53 KB
Copy raw file
Download raw file
Outline
Edit and raw actions

Strings

Strings represent text in Python. Strings in Python are encoded in Unicode, which means their reach extends far beyond ASCII. You can use Chinese characters, Arabic characters, and more.

String Literals

To define a string literal, you can enclose text in either single-quotes or double-quotes, but you should stay consistent. You can use single-quotes inside a string enclosed in double-quotes and vice-versa.

'this is a string declaration'

this is a string declaration

'șʈȓЇиǵʂ ářƏ ūŋĭçóďę abcABC123!@#'

șʈȓЇиǵʂ ářƏ ūŋĭçóďę abcABC123!@#

"we can also use double quotes"

we can also use double quotes

'using single-quotes "allows" you to use double-quotes inside'

using single-quotes "allows" you to use double-quotes inside

"and 'vice versa' with double-quotes"

and 'vice versa' with double-quotes

Escape Sequences: '\n', '\t', etc

Escape sequences allow us to define special characters within strings.

  • \' allows you to have a single quote inside a string enclosed in single quotes
  • \" allows you to have a double-quote inside a string enclosed in double-quotes
  • \n represents a new-line
  • \t represents a tab
  • \\ represents a backslash
  • \xhhhh represents a unicode character with id 'hhhh', e.g. \u0394
'we can use \'single quotes\' in a single-quoted string'

we can use 'single quotes' in a single-quoted string

"we can use \"double quotes\" in a double-quoted string"

we can use "double quotes" in a double-quoted string

'we can also use a line break\na\ttab\nand a backslash \\\nand this \u0394'

we can also use a line break a tab and a backslash
and this Δ

Raw Strings

Prefixing a string with 'r' will ignore any escape sequences and interpret the string literally.

r'this is a raw string \n\t\\'

this is a raw string \n\t\\

Ascii Codes: ord and chr

There are two built-in functions for switching back and forth between characters and their ascii codes: chr and ord. You can find more information about these in the official docs for chr and ord. You can view the ASCII table here

print(ord('a')) # 97
print(chr(97))) # a

String Operations

Remember that strings are immutable meaning their values cannot be changed. Each of these operations returns a new string. You can find some reasons why strings are immutable here.

Concatenation: a + b, a += b

Combine two strings into one

'hello' + ' ' + 'world'

hello world

s = 'hello'
s += ' world'
print(s)

hello world

Multiplication: a * b, a *= b

Repeat a string

s = 'hello! '
print('hello! ' * 4)

hello! hello! hello! hello!

Length: len(a)

Get the length of a string

s = 'hello world!'
print(len(s))

12

Access: a[i]

Get the character at a given index, indices start at 0.

#    012345
s = 'hello!'
print(s[0]) # h
print(s[4]) # o

Slicing: a[i:j], a[i:j:k]

Slicing allows you to extract a portion of a string. The syntax is string[start_index : end_index : increment].

#    0123456789..
s = 'hello world!'
print(s[3:9]) # lo wor

If you leave out the starting index, it's implied to be the start of the string.

#    0123456789..
s = 'hello world!'
print(s[:9]) # hello wor

If you leave out the ending index, it's implied to be the end of the string.

#    0123456789..
s = 'hello world!'
print(s[9:]) # ld

You may optionally add a third number, the increment. The code below will yield every other character.

#    0123456789..
s = 'hello world!'
print(s[::2]) # hlowrd

You can use the increment to reverse a string.

#    0123456789..
s = 'hello world!'
print(s[::-1]) # dlrow olleh

Find: a.find(b)

s.find(a) returns the index of a the first occurance of a

print('hello world'.find('l'))

2

Converting Case: a.upper(), a.lower(), a.title()

s.upper() converts to upper case

print('hello'.upper()) # HELLO

s.lower() converts to lower case

print('Hello'.lower()) # hello

s.title() makes the first letter of each word a capital

print('hello world'.title()) # Hello World

Starts-with and Ends-with: startswith, endswith

a.startswith(b) returns true if a starts with b

print('hello world'.startswith('hello')) # True
print('hello world'.startswith('world')) # False

a.endswith(b) returns true if the string ends with b

print('hello world'.endswith('hello')) # False
print('hello world'.endswith('world')) # True

Replace a.replace(b, c)

a.replace(b, c) replaces occurances of string b with string c

print('hello world'.replace('hello', 'goodbye')) # goodbye world

Strip a.strip()

s.strip()removes leading and trailing characters, if given no parameter, it'll strip whitespace

print('   hello\t\n  '.strip())

hello

print('__%__hello_world__%__'.strip('_%')) # hello_world

Split: a.split(b)

s.split(delimeter) splits a string into a list, if no parameter is given, it'll split on whitespace

fruits = 'apples, bananas, pears'
print(fruits.split(', ')) # ['apples', 'bananas', 'pears']

Join: a.join(b)

delimeter.join(list) combines the elements of a list into a single string, separated by the delimeter

print(' - '.join(['apples', 'bananas', 'pears']) # apples - bananas - pears

Count: a.count(b)

print('abcacba'.count('a')) # 3

f-strings

We can use f-strings to quickly format text with variables, f-strings are prefixed with an f, and contain curly braces {} to include variables or even expressions.

a = 'one'
b = 2
print(f'a is {a} and b is {b}') # a is one and b is 2
print(f'1+1={1+1}) # 1+1=2

in

if 'h' in 'hello world!':
  print('success!')

success!

for char in text:

text = 'Hey!'
if char in text:
  print(char)

H e y !

Docstrings

Docstrings are a special kind of multi-line string that's used for generating documentation. You can read more here

Morty Proxy This is a proxified and sanitized view of the page, visit original site.