or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

c.mdgolang.mdindex.mdphp.mdpython.mdruby.md
tile.json

python.mddocs/

0

# Python Functions

1

2

Python standard library functions ported to JavaScript. This module contains 5 string-related constants and functions from Python's string module.

3

4

## Capabilities

5

6

### String Module (5 functions/constants)

7

8

String constants and utility functions from Python's string standard library module.

9

10

```javascript { .api }

11

/**

12

* Python string module constants and functions

13

*/

14

python.string.ascii_letters() // Function returning all ASCII letters

15

python.string.ascii_lowercase() // Function returning lowercase ASCII letters

16

python.string.ascii_uppercase() // Function returning uppercase ASCII letters

17

python.string.capwords(s) // Capitalize words in string (whitespace separated)

18

python.string.punctuation() // Function returning ASCII punctuation characters

19

```

20

21

**Usage Examples:**

22

23

```javascript

24

const locutus = require('locutus');

25

26

// String constants (functions that return strings)

27

console.log(locutus.python.string.ascii_letters());

28

// 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

29

30

console.log(locutus.python.string.ascii_lowercase());

31

// 'abcdefghijklmnopqrstuvwxyz'

32

33

console.log(locutus.python.string.ascii_uppercase());

34

// 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

35

36

console.log(locutus.python.string.punctuation());

37

// '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

38

39

// Capitalize words

40

const capitalized = locutus.python.string.capwords('hello world'); // 'Hello World'

41

```

42

43

## Function Details

44

45

### ascii_letters

46

47

A function that returns a string containing all ASCII letters (both lowercase and uppercase).

48

49

```javascript { .api }

50

/**

51

* Function returning string containing all ASCII letters

52

* @returns {string} All ASCII letters

53

*/

54

python.string.ascii_letters() // Returns 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

55

```

56

57

**Returns:**

58

- (string): `'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'`

59

60

### ascii_lowercase

61

62

A function that returns a string containing lowercase ASCII letters.

63

64

```javascript { .api }

65

/**

66

* Function returning lowercase ASCII letters

67

* @returns {string} Lowercase ASCII letters

68

*/

69

python.string.ascii_lowercase() // Returns 'abcdefghijklmnopqrstuvwxyz'

70

```

71

72

**Returns:**

73

- (string): `'abcdefghijklmnopqrstuvwxyz'`

74

75

### ascii_uppercase

76

77

A function that returns a string containing uppercase ASCII letters.

78

79

```javascript { .api }

80

/**

81

* Function returning uppercase ASCII letters

82

* @returns {string} Uppercase ASCII letters

83

*/

84

python.string.ascii_uppercase() // Returns 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

85

```

86

87

**Returns:**

88

- (string): `'ABCDEFGHIJKLMNOPQRSTUVWXYZ'`

89

90

### punctuation

91

92

A function that returns a string containing ASCII punctuation characters.

93

94

```javascript { .api }

95

/**

96

* Function returning ASCII punctuation characters

97

* @returns {string} ASCII punctuation characters

98

*/

99

python.string.punctuation() // Returns '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

100

```

101

102

**Returns:**

103

- (string): `'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'`

104

105

### capwords(s)

106

107

Return a capitalized version of the string where words are separated by whitespace.

108

109

```javascript { .api }

110

/**

111

* Return a capitalized version of the string

112

* @param {string} s - The string to capitalize

113

* @returns {string} String with capitalized words

114

*/

115

python.string.capwords(s)

116

```

117

118

**Parameters:**

119

- `s` (string): The string to capitalize

120

121

**Returns:**

122

- (string): A string with the first character of each word capitalized

123

124

**Examples:**

125

```javascript

126

const locutus = require('locutus');

127

128

// Whitespace separation

129

locutus.python.string.capwords('hello world'); // 'Hello World'

130

locutus.python.string.capwords('the quick brown fox'); // 'The Quick Brown Fox'

131

locutus.python.string.capwords(' hello world '); // 'Hello World '

132

133

// Edge cases

134

locutus.python.string.capwords(''); // ''

135

locutus.python.string.capwords('hello'); // 'Hello'

136

locutus.python.string.capwords('HELLO WORLD'); // 'HELLO WORLD'

137

```

138

139

## Use Cases

140

141

Python string functions are useful for:

142

143

- **Character Set Validation**: Checking if strings contain only specific character types

144

- **Text Processing**: Capitalizing text with custom separators

145

- **Data Sanitization**: Filtering out unwanted characters using punctuation constants

146

- **Form Validation**: Validating input against allowed character sets

147

- **Template Processing**: Using constants for character class matching

148

149

```javascript

150

const locutus = require('locutus');

151

152

// Validate that string contains only letters

153

function isAlphabetic(text) {

154

const letters = locutus.python.string.ascii_letters();

155

return text.split('').every(char => letters.includes(char));

156

}

157

158

// Remove punctuation from text

159

function removePunctuation(text) {

160

const punct = locutus.python.string.punctuation();

161

return text.split('').filter(char => !punct.includes(char)).join('');

162

}

163

164

// Generate random string from character set

165

function randomString(length, charSet = locutus.python.string.ascii_letters()) {

166

let result = '';

167

for (let i = 0; i < length; i++) {

168

result += charSet.charAt(Math.floor(Math.random() * charSet.length));

169

}

170

return result;

171

}

172

173

// Capitalize text (whitespace separated only)

174

function capitalizeText(text) {

175

return locutus.python.string.capwords(text);

176

}

177

178

// Examples

179

console.log(isAlphabetic('HelloWorld')); // true

180

console.log(isAlphabetic('Hello123')); // false

181

console.log(removePunctuation('Hello, World!')); // 'Hello World'

182

console.log(randomString(8)); // e.g., 'aBcDeFgH'

183

console.log(capitalizeText('hello world test')); // 'Hello World Test'

184

```

185

186

## Import Patterns

187

188

```javascript

189

// Full module access

190

const locutus = require('locutus');

191

locutus.python.string.capwords('hello world');

192

console.log(locutus.python.string.ascii_letters());

193

194

// Individual function import

195

const capwords = require('locutus/python/string/capwords');

196

const ascii_letters = require('locutus/python/string/ascii_letters');

197

const punctuation = require('locutus/python/string/punctuation');

198

199

capwords('hello world'); // 'Hello World'

200

console.log(ascii_letters()); // 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

201

console.log(punctuation()); // '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

202

```