GUID
string length
unique identifier
programming
UUID

What is the string length of a GUID?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

The standard string representation of a GUID (Globally Unique Identifier) is 36 characters long: 32 hexadecimal digits plus 4 hyphens, formatted as xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. However, the exact length depends on which format you use. Without hyphens it is 32 characters, with braces it is 38, and as a Base64 string it is 22-24 characters. Knowing these lengths matters for database column sizing, API validation, and input parsing.

GUID Structure and Format

A GUID is a 128-bit value. It is the same thing as a UUID (Universally Unique Identifier) defined by RFC 4122. The terms are used interchangeably, though "GUID" is more common in Microsoft ecosystems and "UUID" in Linux, Java, and web contexts.

The 128 bits are divided into five groups when displayed as a string:

 
18-4-4-4-12 hexadecimal digits
2
3Example: 550e8400-e29b-41d4-a716-446655440000
4         ^^^^^^^^ ^^^^ ^^^^ ^^^^ ^^^^^^^^^^^^
5         8 chars  4    4    4    12 chars

The math:

 
32 hex digits + 4 hyphens = 36 characters

Each hexadecimal digit represents 4 bits, so 32 hex digits = 128 bits.

All Standard String Formats and Their Lengths

Different platforms and APIs use different string representations. Here is every common format with its exact length.

Format NameExampleLengthUsed By
Standard (D format)550e8400-e29b-41d4-a716-44665544000036Most APIs, databases, RFC 4122
No hyphens (N format)550e8400e29b41d4a71644665544000032Compact storage, URLs
Braces (B format){550e8400-e29b-41d4-a716-446655440000}38Windows Registry, COM
Parentheses (P format)(550e8400-e29b-41d4-a716-446655440000)38Rare, some legacy systems
Hex (X format){0x550e8400,0xe29b,0x41d4,{0xa7,0x16,...}}VariableC/C++ struct initialization
URNurn:uuid:550e8400-e29b-41d4-a716-44665544000045XML, SOAP, URN namespaces
Base64VQ6EAOKbQdSnFkRmVUQAAA==24Compact binary-safe encoding
Base64url (no padding)VQ6EAOKbQdSnFkRmVUQAAA22JWT, URL-safe contexts

The 36-character format (with hyphens, no braces) is by far the most common and is what most developers mean when they ask about GUID string length.

Generating GUIDs and Checking Length in Code

C# / .NET

csharp
1using System;
2
3Guid id = Guid.NewGuid();
4
5Console.WriteLine(id.ToString());     // 36 chars: 550e8400-e29b-41d4-a716-446655440000
6Console.WriteLine(id.ToString("D"));  // 36 chars: same as above (D is default)
7Console.WriteLine(id.ToString("N"));  // 32 chars: 550e8400e29b41d4a716446655440000
8Console.WriteLine(id.ToString("B"));  // 38 chars: {550e8400-e29b-41d4-a716-446655440000}
9Console.WriteLine(id.ToString("P"));  // 38 chars: (550e8400-e29b-41d4-a716-446655440000)
10
11// Verify lengths
12Console.WriteLine(id.ToString("D").Length);  // 36
13Console.WriteLine(id.ToString("N").Length);  // 32
14Console.WriteLine(id.ToString("B").Length);  // 38

Java

java
1import java.util.UUID;
2
3UUID id = UUID.randomUUID();
4String str = id.toString();
5
6System.out.println(str);           // 550e8400-e29b-41d4-a716-446655440000
7System.out.println(str.length());  // 36
8
9// Without hyphens
10String compact = str.replace("-", "");
11System.out.println(compact.length());  // 32

Python

python
1import uuid
2
3id = uuid.uuid4()
4
5print(str(id))          # 550e8400-e29b-41d4-a716-446655440000
6print(len(str(id)))     # 36
7
8print(id.hex)           # 550e8400e29b41d4a716446655440000
9print(len(id.hex))      # 32
10
11# Bytes representation
12print(len(id.bytes))    # 16 (raw 128-bit value)

JavaScript / TypeScript

javascript
1// Using the 'uuid' package (npm install uuid)
2import { v4 as uuidv4 } from 'uuid';
3
4const id = uuidv4();
5console.log(id);          // 550e8400-e29b-41d4-a716-446655440000
6console.log(id.length);   // 36
7
8// Using crypto.randomUUID() (Node 19+, modern browsers)
9const id2 = crypto.randomUUID();
10console.log(id2.length);  // 36

SQL (PostgreSQL)

sql
1-- UUID type stores as 128-bit binary internally
2CREATE TABLE users (
3    id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
4    name TEXT NOT NULL
5);
6
7-- String length when cast
8SELECT length(gen_random_uuid()::text);  -- 36

SQL (SQL Server)

sql
1-- UNIQUEIDENTIFIER type
2CREATE TABLE users (
3    id UNIQUEIDENTIFIER DEFAULT NEWID() PRIMARY KEY,
4    name NVARCHAR(100) NOT NULL
5);
6
7-- When stored as string, use 36 chars (or 38 with braces)
8SELECT LEN(CAST(NEWID() AS NVARCHAR(36)));  -- 36

Database Column Sizing

When storing GUIDs as strings (rather than native UUID types), use the correct column length.

DatabaseNative TypeString Column Size
PostgreSQLUUID (16 bytes)CHAR(36) or VARCHAR(36)
MySQLNo native UUID typeCHAR(36) or BINARY(16)
SQL ServerUNIQUEIDENTIFIER (16 bytes)CHAR(36) or NCHAR(36)
SQLiteNo native UUID typeTEXT (no fixed size needed)

Use CHAR(36) rather than VARCHAR(36) when every value is exactly 36 characters. Fixed-length columns avoid per-row length metadata and can be slightly faster for lookups. If your application might store both the 36-character and 32-character formats, use VARCHAR(36).

When the database supports a native UUID type (PostgreSQL, SQL Server), prefer it over string storage. Native types store 16 bytes instead of 36, saving disk space and improving index performance.

UUID Versions

The version number is encoded in the 13th character of the string (the M position in xxxxxxxx-xxxx-Mxxx-xxxx-xxxxxxxxxxxx). The string length is the same for all versions.

VersionGeneration MethodExample (13th char)
1Timestamp + MAC address1
3MD5 hash of name + namespace3
4Random4
5SHA-1 hash of name + namespace5
7Unix timestamp + random (newer, sortable)7

Version 4 (random) is the most widely used. Version 7 is gaining adoption because its timestamp prefix makes UUIDs sortable, which improves database index performance compared to fully random Version 4 UUIDs.

Validation Regex

When accepting GUIDs as input, validate the format to prevent injection or malformed data.

python
1import re
2
3# Standard 36-character format (case-insensitive)
4GUID_PATTERN = re.compile(
5    r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$',
6    re.IGNORECASE
7)
8
9def is_valid_guid(value: str) -> bool:
10    return bool(GUID_PATTERN.match(value))
11
12print(is_valid_guid("550e8400-e29b-41d4-a716-446655440000"))  # True
13print(is_valid_guid("not-a-guid"))                              # False
14print(is_valid_guid("550e8400e29b41d4a716446655440000"))        # False (no hyphens)
javascript
1// JavaScript
2const GUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
3
4function isValidGuid(value) {
5  return GUID_REGEX.test(value);
6}

Common Pitfalls

  • Sizing a database column to 32 characters. The standard format is 36 characters (with hyphens). A CHAR(32) column silently truncates the GUID and causes lookup failures.
  • Comparing GUIDs as case-sensitive strings. Hex digits a-f and A-F are equivalent. 550e8400... and 550E8400... represent the same GUID. Use case-insensitive comparison or normalize to lowercase before storing.
  • Assuming all GUID strings have hyphens. Some systems output the 32-character format without hyphens, or the 38-character format with braces. Parse defensively if you accept GUIDs from external sources.
  • Storing GUIDs as strings when a native UUID column type is available. String storage uses 36 bytes minimum (plus encoding overhead) versus 16 bytes for a native type. On tables with millions of rows, this difference in index size is significant.
  • Using Version 4 UUIDs as primary keys in B-tree indexes without considering insert performance. Random UUIDs scatter inserts across the index, causing page splits. Version 7 (timestamp-prefixed) UUIDs or ULID are better choices for ordered indexes.
  • Confusing GUID byte order. Microsoft's GUID struct uses mixed-endian byte order (first three groups are little-endian, last two are big-endian). When converting between string and byte representations across platforms, byte order mismatches produce different GUIDs from the same string.

Summary

  • The standard string length of a GUID is 36 characters (32 hex digits + 4 hyphens).
  • Without hyphens: 32 characters. With braces: 38 characters. As Base64: 22-24 characters.
  • Size database string columns as CHAR(36) or use a native UUID type when available.
  • The underlying data is always 128 bits (16 bytes) regardless of string format.
  • GUIDs and UUIDs are the same thing. Version 4 (random) is the most common; Version 7 (timestamp-sortable) is the modern recommendation for database primary keys.
  • Validate format with a regex and compare case-insensitively when matching GUIDs as strings.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions