Built-In functions and Methods

Python tutorial · PySpark.in

String Built-In Functions: len(), chr(), ord(), str()

1. len():it returns the length of a string

```

s = "hello"

print(len(s)) # 5

```

2. chr(): it Converts Unicode number to character

```

print(chr(65)) # A

print(chr(128512)) # 😀

```

3. ord(): it Converts character to Unicode number

```

print(ord('A')) # 65

print(ord('😊')) # 128522

```

4. str():it Converts object to string

```

print(str(123)) # '123'

print(str(3.14)) # '3.14'

```

String Methods

Below are the most important Python string methods explained with clear examples.

1. capitalize():Converts the first character to uppercase and the rest to lowercase.

```

print("python programming".capitalize())

# Python programming

```

2. casefold():Converts string into aggressive lowercase for case-insensitive comparisons.

Aggressive lowercase (useful for comparisons).

```

print("ß".casefold()) # ss

```

3. center(width): Returns the string centered in a field of given width with optional fill character.

```

print("hello".center(20, '*'))

```

4. count():Returns the number of occurrences of a substring.

```

print("banana".count("a")) # 3

```

5. encode():Converts the string into a bytes object using the given encoding.

```

print("hello".encode("utf-8"))

# b'hello'

```

6. endswith():Checks if the string ends with the specified suffix.

```

print("notes.pdf".endswith(".pdf")) # True

```

7. find():Returns the index of the first occurrence of a substring or -1 if not found. Returns index or -1.

```

print("hello".find("l")) # 2

```

8. format():Inserts values into placeholders {} using the format specification.

```

print("Hello {}".format("Megha"))

```

9. index():Returns index of substring but raises an error if not found. Like find() but raises error if not found.

```

print("hello".index("e")) # 1

```

10. isalnum(), isalpha(), isdigit(), isnumeric(), isspace()

Returns True if all characters are alphanumeric.

```

"abc123".isalnum() # True

"abc".isalpha() # True

"123".isdigit() # True

" ".isspace() # True

```

11. join():Joins items of an iterable into a single string separated by the current string.

```

names = ["A", "B", "C"]

print("-".join(names))

# A-B-C

```

12. lower(), upper(), title(), capitalize():Converts string to lowercase / uppercase / title case / capitalized case.

```

s = "hello world"

print(s.upper()) # HELLO WORLD

print(s.title()) # Hello World

```

13. lstrip(), rstrip(), strip():Removes leading / trailing / both-side whitespace (or specified characters).

```

print(" hello ".strip()) # hello

```

14. partition():Splits string into a tuple: (before sep, sep, after sep).

```

print("a-b-c".partition("-"))

# ('a', '-', 'b-c')

```

15. replace():Replaces all occurrences of a substring with another substring.

```

print("hello world".replace("world", "Python"))

```

16. rfind():Returns the last occurrence index of a substring or -1 if not found.

```

print("hello".rfind("l")) # 3

```

17. split():Splits string into a list based on a separator.

```

print("a,b,c".split(","))

# ['a', 'b', 'c']

```

18. splitlines():Splits the string at line breaks into a list of lines.

```

print("A\nB\nC".splitlines())

# ['A', 'B', 'C']

```

19. startswith():Checks if the string begins with the specified prefix.

```

print("python".startswith("py"))

```

20. swapcase():Swaps uppercase characters to lowercase and vice-versa.

```

print("HeLLo".swapcase()) # hEllO

```

21. zfill():Pads the string on the left with zeros to reach the given width.

```

print("42".zfill(5)) # 00042

```

More Python tutorials

All tutorials · Try the free PySpark compiler · Practice challenges